1 /*
2 * Copyright (c) 2008, 2023, 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/vmClasses.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "oops/method.hpp"
31 #include "runtime/fieldDescriptor.hpp"
32 #include "runtime/frame.hpp"
33 #include "runtime/globals.hpp"
34 #include "runtime/stubCodeGenerator.hpp"
35 #include "utilities/macros.hpp"
36
37 #ifdef ZERO
38 # include "entry_zero.hpp"
39 # include "interpreter/interpreter.hpp"
40 #endif
41
42 class MacroAssembler;
43 class MethodHandlesAdapterBlob;
44 class Label;
45
46 class MethodHandles: AllStatic {
47 // JVM support for MethodHandle, MethodType, and related types
48 // in java.lang.invoke and sun.invoke.
49 // See also javaClasses for layouts java_lang_invoke_Method{Handle,Type,Type::Form}.
50 public:
51 public:
52 static bool enabled() { return _enabled; }
53 static void set_enabled(bool z);
54
55 private:
56 static bool _enabled;
57
58 // Adapters.
59 static MethodHandlesAdapterBlob* _adapter_code;
60
61 // utility functions for reifying names and types
62 static oop field_name_or_null(Symbol* s);
63 static oop field_signature_type_or_null(Symbol* s);
64
65 public:
66 // working with member names
67 static Handle resolve_MemberName(Handle mname, Klass* caller, int lookup_mode,
68 bool speculative_resolve, TRAPS); // compute vmtarget/vmindex from name/type
69 static void expand_MemberName(Handle mname, int suppress, TRAPS); // expand defc/name/type if missing
70 static oop init_MemberName(Handle mname_h, Handle target_h, TRAPS); // compute vmtarget/vmindex from target
71 static oop init_field_MemberName(Handle mname_h, fieldDescriptor& fd, bool is_setter = false);
72 static oop init_method_MemberName(Handle mname_h, CallInfo& info);
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 clean_dependency_context(oop call_site);
81
82 static void mark_dependent_nmethods(DeoptimizationScope* deopt_scope, Handle call_site, Handle target);
83
84 // Generate MethodHandles adapters.
85 static void generate_adapters();
86
87 // Called from MethodHandlesAdapterGenerator.
88 static address generate_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid);
89 static void generate_method_handle_dispatch(MacroAssembler* _masm,
90 vmIntrinsics::ID iid,
91 Register receiver_reg,
92 Register member_reg,
93 bool for_compiler_entry);
94
95 // Queries
96 static bool is_signature_polymorphic(vmIntrinsics::ID iid) {
97 return (iid >= vmIntrinsics::FIRST_MH_SIG_POLY &&
98 iid <= vmIntrinsics::LAST_MH_SIG_POLY);
99 }
100
101 static bool is_signature_polymorphic_method(Method* m) {
102 return is_signature_polymorphic(m->intrinsic_id());
103 }
104
105 static bool is_signature_polymorphic_intrinsic(vmIntrinsics::ID iid) {
106 assert(is_signature_polymorphic(iid), "");
107 // Most sig-poly methods are intrinsics which do not require an
108 // appeal to Java for adapter code.
109 return (iid != vmIntrinsics::_invokeGeneric);
110 }
111
112 static bool is_signature_polymorphic_static(vmIntrinsics::ID iid) {
113 assert(is_signature_polymorphic(iid), "");
114 return (iid >= vmIntrinsics::FIRST_MH_STATIC &&
115 iid <= vmIntrinsics::LAST_MH_SIG_POLY);
116 }
117
118 static bool has_member_arg(vmIntrinsics::ID iid) {
119 assert(is_signature_polymorphic(iid), "");
120 return (iid >= vmIntrinsics::_linkToVirtual &&
121 iid <= vmIntrinsics::_linkToNative);
122 }
123 static bool has_member_arg(Symbol* klass, Symbol* name) {
124 if ((klass == vmSymbols::java_lang_invoke_MethodHandle() ||
125 klass == vmSymbols::java_lang_invoke_VarHandle()) &&
126 is_signature_polymorphic_name(name)) {
127 vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
128 return has_member_arg(iid);
129 }
130 return false;
131 }
132
133 static Symbol* signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid);
134 static int signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid);
135
136 static vmIntrinsics::ID signature_polymorphic_name_id(Klass* klass, Symbol* name);
137 static vmIntrinsics::ID signature_polymorphic_name_id(Symbol* name);
138 static bool is_signature_polymorphic_name(Symbol* name) {
139 return signature_polymorphic_name_id(name) != vmIntrinsics::_none;
140 }
141 static bool is_method_handle_invoke_name(Klass* klass, Symbol* name);
142 static bool is_signature_polymorphic_name(Klass* klass, Symbol* name) {
143 return signature_polymorphic_name_id(klass, name) != vmIntrinsics::_none;
144 }
145 static bool is_signature_polymorphic_public_name(Klass* klass, Symbol* name);
146
147 static Bytecodes::Code signature_polymorphic_intrinsic_bytecode(vmIntrinsics::ID id);
148
149 public:
150 static Symbol* lookup_signature(oop type_str, bool polymorphic, TRAPS); // use TempNewSymbol
151 static Symbol* lookup_basic_type_signature(Symbol* sig, bool keep_last_arg); // use TempNewSymbol
152 static Symbol* lookup_basic_type_signature(Symbol* sig) {
153 return lookup_basic_type_signature(sig, false);
154 }
155 static bool is_basic_type_signature(Symbol* sig);
156
157 static void print_as_basic_type_signature_on(outputStream* st, Symbol* sig);
158
159 // decoding CONSTANT_MethodHandle constants
160 enum { JVM_REF_MIN = JVM_REF_getField, JVM_REF_MAX = JVM_REF_invokeInterface };
161 static bool ref_kind_is_valid(int ref_kind) {
162 return (ref_kind >= JVM_REF_MIN && ref_kind <= JVM_REF_MAX);
163 }
164 static bool ref_kind_is_field(int ref_kind) {
165 assert(ref_kind_is_valid(ref_kind), "");
166 return (ref_kind <= JVM_REF_putStatic);
167 }
168 static bool ref_kind_is_getter(int ref_kind) {
169 assert(ref_kind_is_valid(ref_kind), "");
170 return (ref_kind <= JVM_REF_getStatic);
171 }
172 static bool ref_kind_is_setter(int ref_kind) {
173 return ref_kind_is_field(ref_kind) && !ref_kind_is_getter(ref_kind);
174 }
175 static bool ref_kind_is_method(int ref_kind) {
176 return !ref_kind_is_field(ref_kind) && (ref_kind != JVM_REF_newInvokeSpecial);
177 }
178 static bool ref_kind_has_receiver(int ref_kind) {
179 assert(ref_kind_is_valid(ref_kind), "");
180 return (ref_kind & 1) != 0;
181 }
182
183 static int ref_kind_to_flags(int ref_kind);
184
185 #include CPU_HEADER(methodHandles)
186
187 // Tracing
188 static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
189 static void trace_method_handle_interpreter_entry(MacroAssembler* _masm, vmIntrinsics::ID iid);
190
191 public:
192 static void init_counters();
193 static void print_counters_on(outputStream* st);
194 };
195
196 //------------------------------------------------------------------------------
197 // MethodHandlesAdapterGenerator
198 //
199 class MethodHandlesAdapterGenerator : public StubCodeGenerator {
200 public:
201 MethodHandlesAdapterGenerator(CodeBuffer* code) : StubCodeGenerator(code, PrintMethodHandleStubs) {}
202
203 void generate();
204 };
205
206 #endif // SHARE_PRIMS_METHODHANDLES_HPP