| 1 | /* | 
|---|
| 2 | * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. | 
|---|
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 
|---|
| 4 | * | 
|---|
| 5 | * This code is free software; you can redistribute it and/or modify it | 
|---|
| 6 | * under the terms of the GNU General Public License version 2 only, as | 
|---|
| 7 | * published by the Free Software Foundation. | 
|---|
| 8 | * | 
|---|
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
|---|
| 11 | * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License | 
|---|
| 12 | * version 2 for more details (a copy is included in the LICENSE file that | 
|---|
| 13 | * accompanied this code). | 
|---|
| 14 | * | 
|---|
| 15 | * You should have received a copy of the GNU General Public License version | 
|---|
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, | 
|---|
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | 
|---|
| 18 | * | 
|---|
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | 
|---|
| 20 | * or visit www.oracle.com if you need additional information or have any | 
|---|
| 21 | * questions. | 
|---|
| 22 | * | 
|---|
| 23 | */ | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef SHARE_PRIMS_METHODHANDLES_HPP | 
|---|
| 26 | #define SHARE_PRIMS_METHODHANDLES_HPP | 
|---|
| 27 |  | 
|---|
| 28 | #include "classfile/javaClasses.hpp" | 
|---|
| 29 | #include "classfile/vmSymbols.hpp" | 
|---|
| 30 | #include "runtime/frame.hpp" | 
|---|
| 31 | #include "runtime/globals.hpp" | 
|---|
| 32 | #include "utilities/macros.hpp" | 
|---|
| 33 |  | 
|---|
| 34 | #ifdef ZERO | 
|---|
| 35 | # include "entry_zero.hpp" | 
|---|
| 36 | # include "interpreter/interpreter.hpp" | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | class MacroAssembler; | 
|---|
| 41 | class Label; | 
|---|
| 42 |  | 
|---|
| 43 | class MethodHandles: AllStatic { | 
|---|
| 44 | // JVM support for MethodHandle, MethodType, and related types | 
|---|
| 45 | // in java.lang.invoke and sun.invoke. | 
|---|
| 46 | // See also  javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}. | 
|---|
| 47 | public: | 
|---|
| 48 | public: | 
|---|
| 49 | static bool enabled()                         { return _enabled; } | 
|---|
| 50 | static void set_enabled(bool z); | 
|---|
| 51 |  | 
|---|
| 52 | private: | 
|---|
| 53 | static bool _enabled; | 
|---|
| 54 |  | 
|---|
| 55 | // Adapters. | 
|---|
| 56 | static MethodHandlesAdapterBlob* _adapter_code; | 
|---|
| 57 |  | 
|---|
| 58 | // utility functions for reifying names and types | 
|---|
| 59 | static oop field_name_or_null(Symbol* s); | 
|---|
| 60 | static oop field_signature_type_or_null(Symbol* s); | 
|---|
| 61 |  | 
|---|
| 62 | public: | 
|---|
| 63 | // working with member names | 
|---|
| 64 | static Handle resolve_MemberName(Handle mname, Klass* caller, | 
|---|
| 65 | bool speculative_resolve, TRAPS); // compute vmtarget/vmindex from name/type | 
|---|
| 66 | static void expand_MemberName(Handle mname, int suppress, TRAPS);  // expand defc/name/type if missing | 
|---|
| 67 | static oop init_MemberName(Handle mname_h, Handle target_h, TRAPS); // compute vmtarget/vmindex from target | 
|---|
| 68 | static oop init_field_MemberName(Handle mname_h, fieldDescriptor& fd, bool is_setter = false); | 
|---|
| 69 | static oop init_method_MemberName(Handle mname_h, CallInfo& info); | 
|---|
| 70 | static int find_MemberNames(Klass* k, Symbol* name, Symbol* sig, | 
|---|
| 71 | int mflags, Klass* caller, | 
|---|
| 72 | int skip, objArrayHandle results, TRAPS); | 
|---|
| 73 | static Handle resolve_MemberName_type(Handle mname, Klass* caller, TRAPS); | 
|---|
| 74 |  | 
|---|
| 75 | // bit values for suppress argument to expand_MemberName: | 
|---|
| 76 | enum { _suppress_defc = 1, _suppress_name = 2, _suppress_type = 4 }; | 
|---|
| 77 |  | 
|---|
| 78 | // CallSite support | 
|---|
| 79 | static void add_dependent_nmethod(oop call_site, nmethod* nm); | 
|---|
| 80 | static void remove_dependent_nmethod(oop call_site, nmethod* nm); | 
|---|
| 81 | static void clean_dependency_context(oop call_site); | 
|---|
| 82 |  | 
|---|
| 83 | static void flush_dependent_nmethods(Handle call_site, Handle target); | 
|---|
| 84 |  | 
|---|
| 85 | // Generate MethodHandles adapters. | 
|---|
| 86 | static void generate_adapters(); | 
|---|
| 87 |  | 
|---|
| 88 | // Called from MethodHandlesAdapterGenerator. | 
|---|
| 89 | static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid); | 
|---|
| 90 | static void generate_method_handle_dispatch(MacroAssembler* _masm, | 
|---|
| 91 | vmIntrinsics::ID iid, | 
|---|
| 92 | Register receiver_reg, | 
|---|
| 93 | Register member_reg, | 
|---|
| 94 | bool for_compiler_entry); | 
|---|
| 95 |  | 
|---|
| 96 | // Queries | 
|---|
| 97 | static bool is_signature_polymorphic(vmIntrinsics::ID iid) { | 
|---|
| 98 | return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY && | 
|---|
| 99 | iid <= vmIntrinsics::LAST_MH_SIG_POLY); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | static bool is_signature_polymorphic_method(Method* m) { | 
|---|
| 103 | return is_signature_polymorphic(m->intrinsic_id()); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) { | 
|---|
| 107 | assert(is_signature_polymorphic(iid), ""); | 
|---|
| 108 | // Most sig-poly methods are intrinsics which do not require an | 
|---|
| 109 | // appeal to Java for adapter code. | 
|---|
| 110 | return (iid != vmIntrinsics::_invokeGeneric); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) { | 
|---|
| 114 | assert(is_signature_polymorphic(iid), ""); | 
|---|
| 115 | return (iid >= vmIntrinsics::FIRST_MH_STATIC && | 
|---|
| 116 | iid <= vmIntrinsics::LAST_MH_SIG_POLY); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | static bool has_member_arg(vmIntrinsics::ID iid) { | 
|---|
| 120 | assert(is_signature_polymorphic(iid), ""); | 
|---|
| 121 | return (iid >= vmIntrinsics::_linkToVirtual && | 
|---|
| 122 | iid <= vmIntrinsics::_linkToInterface); | 
|---|
| 123 | } | 
|---|
| 124 | static bool has_member_arg(Symbol* klass, Symbol* name) { | 
|---|
| 125 | if ((klass == vmSymbols::java_lang_invoke_MethodHandle() || | 
|---|
| 126 | klass == vmSymbols::java_lang_invoke_VarHandle()) && | 
|---|
| 127 | is_signature_polymorphic_name(name)) { | 
|---|
| 128 | vmIntrinsics::ID iid = signature_polymorphic_name_id(name); | 
|---|
| 129 | return has_member_arg(iid); | 
|---|
| 130 | } | 
|---|
| 131 | return false; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid); | 
|---|
| 135 | static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid); | 
|---|
| 136 |  | 
|---|
| 137 | static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name); | 
|---|
| 138 | static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name); | 
|---|
| 139 | static bool is_signature_polymorphic_name(Symbol* name) { | 
|---|
| 140 | return signature_polymorphic_name_id(name) != vmIntrinsics::_none; | 
|---|
| 141 | } | 
|---|
| 142 | static bool is_method_handle_invoke_name(Klass* klass, Symbol* name); | 
|---|
| 143 | static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) { | 
|---|
| 144 | return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none; | 
|---|
| 145 | } | 
|---|
| 146 | static bool is_signature_polymorphic_public_name(Klass* klass, Symbol* name); | 
|---|
| 147 |  | 
|---|
| 148 | static Bytecodes::Code signature_polymorphic_intrinsic_bytecode(vmIntrinsics::ID id); | 
|---|
| 149 |  | 
|---|
| 150 | public: | 
|---|
| 151 | static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS);  // use TempNewSymbol | 
|---|
| 152 | static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS);  // use TempNewSymbol | 
|---|
| 153 | static Symbol* lookup_basic_type_signature(Symbol* sig, TRAPS) { | 
|---|
| 154 | return lookup_basic_type_signature(sig, false, THREAD); | 
|---|
| 155 | } | 
|---|
| 156 | static bool is_basic_type_signature(Symbol* sig); | 
|---|
| 157 |  | 
|---|
| 158 | static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig, bool keep_arrays = false, bool keep_basic_names = false); | 
|---|
| 159 |  | 
|---|
| 160 | // decoding CONSTANT_MethodHandle constants | 
|---|
| 161 | enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface }; | 
|---|
| 162 | static bool ref_kind_is_valid(int ref_kind) { | 
|---|
| 163 | return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX); | 
|---|
| 164 | } | 
|---|
| 165 | static bool ref_kind_is_field(int ref_kind) { | 
|---|
| 166 | assert(ref_kind_is_valid(ref_kind), ""); | 
|---|
| 167 | return (ref_kind <= JVM_REF_putStatic); | 
|---|
| 168 | } | 
|---|
| 169 | static bool ref_kind_is_getter(int ref_kind) { | 
|---|
| 170 | assert(ref_kind_is_valid(ref_kind), ""); | 
|---|
| 171 | return (ref_kind <= JVM_REF_getStatic); | 
|---|
| 172 | } | 
|---|
| 173 | static bool ref_kind_is_setter(int ref_kind) { | 
|---|
| 174 | return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind); | 
|---|
| 175 | } | 
|---|
| 176 | static bool ref_kind_is_method(int ref_kind) { | 
|---|
| 177 | return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial); | 
|---|
| 178 | } | 
|---|
| 179 | static bool ref_kind_has_receiver(int ref_kind) { | 
|---|
| 180 | assert(ref_kind_is_valid(ref_kind), ""); | 
|---|
| 181 | return (ref_kind & 1) != 0; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | static int ref_kind_to_flags(int ref_kind); | 
|---|
| 185 |  | 
|---|
| 186 | #include CPU_HEADER(methodHandles) | 
|---|
| 187 |  | 
|---|
| 188 | // Tracing | 
|---|
| 189 | static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN; | 
|---|
| 190 | static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid); | 
|---|
| 191 | }; | 
|---|
| 192 |  | 
|---|
| 193 | //------------------------------------------------------------------------------ | 
|---|
| 194 | // MethodHandlesAdapterGenerator | 
|---|
| 195 | // | 
|---|
| 196 | class MethodHandlesAdapterGenerator : public StubCodeGenerator { | 
|---|
| 197 | public: | 
|---|
| 198 | MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {} | 
|---|
| 199 |  | 
|---|
| 200 | void generate(); | 
|---|
| 201 | }; | 
|---|
| 202 |  | 
|---|
| 203 | #endif // SHARE_PRIMS_METHODHANDLES_HPP | 
|---|
| 204 |  | 
|---|