jrose@1145: /* sspitsyn@5178: * Copyright (c) 2008, 2013, 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: #ifndef SHARE_VM_PRIMS_METHODHANDLES_HPP stefank@2314: #define SHARE_VM_PRIMS_METHODHANDLES_HPP stefank@2314: stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "runtime/frame.inline.hpp" stefank@2314: #include "runtime/globals.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: jrose@1145: class MacroAssembler; jrose@1145: class Label; jrose@1145: jrose@1145: class MethodHandles: AllStatic { jrose@1145: // JVM support for MethodHandle, MethodType, and related types jrose@2639: // in java.lang.invoke and sun.invoke. jrose@2639: // See also javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}. jrose@1145: public: jrose@1145: public: jrose@1145: static bool enabled() { return _enabled; } jrose@1145: static void set_enabled(bool z); jrose@1145: jrose@1145: private: jrose@1145: static bool _enabled; jrose@1474: twisti@1734: // Adapters. twisti@1734: static MethodHandlesAdapterBlob* _adapter_code; twisti@1734: drchase@5732: // utility functions for reifying names and types drchase@5732: static oop field_name_or_null(Symbol* s); drchase@5732: static oop field_signature_type_or_null(Symbol* s); drchase@5732: jrose@1145: public: jrose@1145: // working with member names twisti@3969: static Handle resolve_MemberName(Handle mname, TRAPS); // compute vmtarget/vmindex from name/type jrose@1145: static void expand_MemberName(Handle mname, int suppress, TRAPS); // expand defc/name/type if missing jrose@1862: static Handle new_MemberName(TRAPS); // must be followed by init_MemberName sspitsyn@4965: static oop init_MemberName(Handle mname_h, Handle target_h); // compute vmtarget/vmindex from target drchase@5732: static oop init_field_MemberName(Handle mname_h, fieldDescriptor& fd, bool is_setter = false); drchase@5732: static oop init_method_MemberName(Handle mname_h, CallInfo& info); coleenp@4037: static int method_ref_kind(Method* m, bool do_dispatch_if_possible = true); sspitsyn@4965: static int find_MemberNames(KlassHandle k, Symbol* name, Symbol* sig, sspitsyn@4965: int mflags, KlassHandle caller, sspitsyn@4965: int skip, objArrayHandle results); jrose@1145: // bit values for suppress argument to expand_MemberName: jrose@1145: enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 }; jrose@1145: twisti@1734: // Generate MethodHandles adapters. sspitsyn@4965: static void generate_adapters(); twisti@1734: twisti@3969: // Called from MethodHandlesAdapterGenerator. twisti@3969: static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid); twisti@3969: static void generate_method_handle_dispatch(MacroAssembler* _masm, twisti@3969: vmIntrinsics::ID iid, twisti@3969: Register receiver_reg, twisti@3969: Register member_reg, twisti@3969: bool for_compiler_entry); jrose@1145: twisti@3969: // Queries twisti@3969: static bool is_signature_polymorphic(vmIntrinsics::ID iid) { twisti@3969: return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY && twisti@3969: iid <= vmIntrinsics::LAST_MH_SIG_POLY); twisti@3969: } jrose@1145: twisti@3969: static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) { twisti@3969: assert(is_signature_polymorphic(iid), ""); twisti@3969: // Most sig-poly methods are intrinsics which do not require an twisti@3969: // appeal to Java for adapter code. twisti@3969: return (iid != vmIntrinsics::_invokeGeneric); twisti@3969: } twisti@3969: twisti@3969: static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) { twisti@3969: assert(is_signature_polymorphic(iid), ""); twisti@3969: return (iid >= vmIntrinsics::FIRST_MH_STATIC && twisti@3969: iid <= vmIntrinsics::LAST_MH_SIG_POLY); twisti@3969: } twisti@3969: twisti@3969: static bool has_member_arg(vmIntrinsics::ID iid) { twisti@3969: assert(is_signature_polymorphic(iid), ""); twisti@3969: return (iid >= vmIntrinsics::_linkToVirtual && twisti@3969: iid <= vmIntrinsics::_linkToInterface); twisti@3969: } twisti@3969: static bool has_member_arg(Symbol* klass, Symbol* name) { twisti@3969: if ((klass == vmSymbols::java_lang_invoke_MethodHandle()) && twisti@3969: is_signature_polymorphic_name(name)) { twisti@3969: vmIntrinsics::ID iid = signature_polymorphic_name_id(name); twisti@3969: return has_member_arg(iid); twisti@3969: } twisti@3969: return false; twisti@3969: } twisti@3969: twisti@3969: static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid); twisti@3969: static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid); twisti@3969: coleenp@4037: static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name); twisti@3969: static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name); twisti@3969: static bool is_signature_polymorphic_name(Symbol* name) { twisti@3969: return signature_polymorphic_name_id(name) != vmIntrinsics::_none; twisti@3969: } coleenp@4037: static bool is_method_handle_invoke_name(Klass* klass, Symbol* name); coleenp@4037: static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) { twisti@3969: return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none; twisti@3969: } twisti@3969: jrose@1145: enum { jrose@1145: // format of query to getConstant: never@3105: GC_COUNT_GWT = 4, twisti@3969: GC_LAMBDA_SUPPORT = 5 jrose@1145: }; jrose@1145: static int get_named_constant(int which, Handle name_box, TRAPS); jrose@1145: twisti@3969: public: twisti@3969: static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS); // use TempNewSymbol twisti@3969: static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS); // use TempNewSymbol twisti@3969: static Symbol* lookup_basic_type_signature(Symbol* sig, TRAPS) { twisti@3969: return lookup_basic_type_signature(sig, false, THREAD); twisti@3969: } twisti@3969: static bool is_basic_type_signature(Symbol* sig); twisti@3969: twisti@3969: static Symbol* lookup_method_type(Symbol* msig, Handle mtype, TRAPS); twisti@3969: twisti@3969: static void print_as_method_type_on(outputStream* st, Symbol* sig) { twisti@3969: print_as_basic_type_signature_on(st, sig, true, true); twisti@3969: } twisti@3969: static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig, bool keep_arrays = false, bool keep_basic_names = false); twisti@3969: twisti@3969: // decoding CONSTANT_MethodHandle constants twisti@3969: enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface }; twisti@3969: static bool ref_kind_is_valid(int ref_kind) { twisti@3969: return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX); twisti@3969: } twisti@3969: static bool ref_kind_is_field(int ref_kind) { twisti@3969: assert(ref_kind_is_valid(ref_kind), ""); twisti@3969: return (ref_kind <= JVM_REF_putStatic); twisti@3969: } twisti@3969: static bool ref_kind_is_getter(int ref_kind) { twisti@3969: assert(ref_kind_is_valid(ref_kind), ""); twisti@3969: return (ref_kind <= JVM_REF_getStatic); twisti@3969: } twisti@3969: static bool ref_kind_is_setter(int ref_kind) { twisti@3969: return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind); twisti@3969: } twisti@3969: static bool ref_kind_is_method(int ref_kind) { twisti@3969: return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial); twisti@3969: } twisti@3969: static bool ref_kind_has_receiver(int ref_kind) { twisti@3969: assert(ref_kind_is_valid(ref_kind), ""); twisti@3969: return (ref_kind & 1) != 0; twisti@3969: } twisti@3969: static bool ref_kind_is_static(int ref_kind) { twisti@3969: return !ref_kind_has_receiver(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial); twisti@3969: } twisti@3969: static bool ref_kind_does_dispatch(int ref_kind) { twisti@3969: return (ref_kind == JVM_REF_invokeVirtual || twisti@3969: ref_kind == JVM_REF_invokeInterface); jrose@1862: } jrose@1862: never@2895: never@2895: #ifdef TARGET_ARCH_x86 never@2895: # include "methodHandles_x86.hpp" never@2895: #endif never@2895: #ifdef TARGET_ARCH_sparc never@2950: # include "methodHandles_sparc.hpp" never@2895: #endif never@2895: #ifdef TARGET_ARCH_zero never@2950: # include "methodHandles_zero.hpp" never@2895: #endif never@2895: #ifdef TARGET_ARCH_arm bdelsart@2917: # include "methodHandles_arm.hpp" never@2895: #endif never@2895: #ifdef TARGET_ARCH_ppc bdelsart@2917: # include "methodHandles_ppc.hpp" never@2895: #endif twisti@3969: twisti@4158: // Tracing twisti@4158: static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN; twisti@4158: static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid) { twisti@4158: if (TraceMethodHandles) { twisti@4158: const char* name = vmIntrinsics::name_at(iid); twisti@4158: if (*name == '_') name += 1; twisti@4158: const size_t len = strlen(name) + 50; twisti@4158: char* qname = NEW_C_HEAP_ARRAY(char, len, mtInternal); twisti@4158: const char* suffix = ""; twisti@4158: if (is_signature_polymorphic(iid)) { twisti@4158: if (is_signature_polymorphic_static(iid)) twisti@4158: suffix = "/static"; twisti@4158: else twisti@4158: suffix = "/private"; twisti@4158: } twisti@4158: jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix); twisti@4158: trace_method_handle(_masm, qname); twisti@4158: // Note: Don't free the allocated char array because it's used twisti@4158: // during runtime. twisti@4158: } twisti@4158: } jrose@1145: }; jrose@1145: twisti@1734: //------------------------------------------------------------------------------ twisti@1734: // MethodHandlesAdapterGenerator twisti@1734: // twisti@1734: class MethodHandlesAdapterGenerator : public StubCodeGenerator { twisti@1734: public: never@2954: MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {} twisti@1734: twisti@2436: void generate(); twisti@1734: }; stefank@2314: sspitsyn@4965: //------------------------------------------------------------------------------ sspitsyn@4965: // MemberNameTable sspitsyn@4965: // sspitsyn@5178: sspitsyn@4965: class MemberNameTable : public GrowableArray { sspitsyn@4965: public: sspitsyn@5178: MemberNameTable(int methods_cnt); sspitsyn@4965: ~MemberNameTable(); sspitsyn@5178: void add_member_name(int index, jweak mem_name_ref); sspitsyn@5178: oop get_member_name(int index); sspitsyn@4965: sspitsyn@4965: #if INCLUDE_JVMTI sspitsyn@4965: public: sspitsyn@4965: // RedefineClasses() API support: sspitsyn@4965: // If a MemberName refers to old_method then update it sspitsyn@4965: // to refer to new_method. sspitsyn@4965: void adjust_method_entries(Method** old_methods, Method** new_methods, sspitsyn@4965: int methods_length, bool *trace_name_printed); sspitsyn@4965: private: sspitsyn@4965: oop find_member_name_by_method(Method* old_method); sspitsyn@4965: #endif // INCLUDE_JVMTI sspitsyn@4965: }; sspitsyn@4965: stefank@2314: #endif // SHARE_VM_PRIMS_METHODHANDLES_HPP