src/share/vm/prims/methodHandles.cpp

Wed, 11 Jan 2012 17:34:02 -0500

author
phh
date
Wed, 11 Jan 2012 17:34:02 -0500
changeset 3427
94ec88ca68e2
parent 3255
44ce519bc3d1
child 3434
15d394228cfa
permissions
-rw-r--r--

7115199: Add event tracing hooks and Java Flight Recorder infrastructure
Summary: Added a nop tracing infrastructure, JFR makefile changes and other infrastructure used only by JFR.
Reviewed-by: acorn, sspitsyn
Contributed-by: markus.gronlund@oracle.com

jrose@1145 1 /*
twisti@2436 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
jrose@1145 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jrose@1145 4 *
jrose@1145 5 * This code is free software; you can redistribute it and/or modify it
jrose@1145 6 * under the terms of the GNU General Public License version 2 only, as
jrose@1145 7 * published by the Free Software Foundation.
jrose@1145 8 *
jrose@1145 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jrose@1145 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jrose@1145 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jrose@1145 12 * version 2 for more details (a copy is included in the LICENSE file that
jrose@1145 13 * accompanied this code).
jrose@1145 14 *
jrose@1145 15 * You should have received a copy of the GNU General Public License version
jrose@1145 16 * 2 along with this work; if not, write to the Free Software Foundation,
jrose@1145 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jrose@1145 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
jrose@1145 22 *
jrose@1145 23 */
jrose@1145 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/symbolTable.hpp"
jrose@2982 27 #include "compiler/compileBroker.hpp"
stefank@2314 28 #include "interpreter/interpreter.hpp"
never@2920 29 #include "interpreter/oopMapCache.hpp"
stefank@2314 30 #include "memory/allocation.inline.hpp"
stefank@2314 31 #include "memory/oopFactory.hpp"
stefank@2314 32 #include "prims/methodHandles.hpp"
never@2920 33 #include "prims/methodHandleWalk.hpp"
jrose@2982 34 #include "runtime/compilationPolicy.hpp"
stefank@2314 35 #include "runtime/javaCalls.hpp"
stefank@2314 36 #include "runtime/reflection.hpp"
stefank@2314 37 #include "runtime/signature.hpp"
stefank@2314 38 #include "runtime/stubRoutines.hpp"
stefank@2314 39
jrose@1145 40 /*
jrose@1145 41 * JSR 292 reference implementation: method handles
jrose@1145 42 */
jrose@1145 43
jrose@1145 44 bool MethodHandles::_enabled = false; // set true after successful native linkage
jrose@1145 45
jrose@1145 46 MethodHandleEntry* MethodHandles::_entries[MethodHandles::_EK_LIMIT] = {NULL};
jrose@1145 47 const char* MethodHandles::_entry_names[_EK_LIMIT+1] = {
jrose@1474 48 "raise_exception",
jrose@1145 49 "invokestatic", // how a MH emulates invokestatic
jrose@1145 50 "invokespecial", // ditto for the other invokes...
jrose@1145 51 "invokevirtual",
jrose@1145 52 "invokeinterface",
jrose@1145 53 "bound_ref", // these are for BMH...
jrose@1145 54 "bound_int",
jrose@1145 55 "bound_long",
jrose@1145 56 "bound_ref_direct", // (direct versions have a direct methodOop)
jrose@1145 57 "bound_int_direct",
jrose@1145 58 "bound_long_direct",
jrose@1145 59
jrose@1145 60 // starting at _adapter_mh_first:
jrose@1145 61 "adapter_retype_only", // these are for AMH...
jrose@1474 62 "adapter_retype_raw",
jrose@1145 63 "adapter_check_cast",
jrose@1145 64 "adapter_prim_to_prim",
jrose@1145 65 "adapter_ref_to_prim",
jrose@1145 66 "adapter_prim_to_ref",
jrose@1145 67 "adapter_swap_args",
jrose@1145 68 "adapter_rot_args",
jrose@1145 69 "adapter_dup_args",
jrose@1145 70 "adapter_drop_args",
jrose@1145 71 "adapter_collect_args",
jrose@1145 72 "adapter_spread_args",
never@2895 73 "adapter_fold_args",
never@2895 74 "adapter_unused_13",
jrose@1145 75
jrose@1145 76 // optimized adapter types:
jrose@1145 77 "adapter_swap_args/1",
jrose@1145 78 "adapter_swap_args/2",
jrose@1145 79 "adapter_rot_args/1,up",
jrose@1145 80 "adapter_rot_args/1,down",
jrose@1145 81 "adapter_rot_args/2,up",
jrose@1145 82 "adapter_rot_args/2,down",
jrose@1145 83 "adapter_prim_to_prim/i2i",
jrose@1145 84 "adapter_prim_to_prim/l2i",
jrose@1145 85 "adapter_prim_to_prim/d2f",
jrose@1145 86 "adapter_prim_to_prim/i2l",
jrose@1145 87 "adapter_prim_to_prim/f2d",
jrose@1145 88 "adapter_ref_to_prim/unboxi",
jrose@1145 89 "adapter_ref_to_prim/unboxl",
never@2895 90
never@2895 91 // return value handlers for collect/filter/fold adapters:
never@2895 92 "return/ref",
never@2895 93 "return/int",
never@2895 94 "return/long",
never@2895 95 "return/float",
never@2895 96 "return/double",
never@2895 97 "return/void",
never@2895 98 "return/S0/ref",
never@2895 99 "return/S1/ref",
never@2895 100 "return/S2/ref",
never@2895 101 "return/S3/ref",
never@2895 102 "return/S4/ref",
never@2895 103 "return/S5/ref",
never@2895 104 "return/any",
never@2895 105
never@2895 106 // spreading (array length cases 0, 1, ...)
never@2895 107 "adapter_spread/0",
never@2895 108 "adapter_spread/1/ref",
never@2895 109 "adapter_spread/2/ref",
never@2895 110 "adapter_spread/3/ref",
never@2895 111 "adapter_spread/4/ref",
never@2895 112 "adapter_spread/5/ref",
never@2895 113 "adapter_spread/ref",
never@2895 114 "adapter_spread/byte",
never@2895 115 "adapter_spread/char",
never@2895 116 "adapter_spread/short",
never@2895 117 "adapter_spread/int",
never@2895 118 "adapter_spread/long",
never@2895 119 "adapter_spread/float",
never@2895 120 "adapter_spread/double",
never@2895 121
never@2895 122 // blocking filter/collect conversions:
never@2895 123 "adapter_collect/ref",
never@2895 124 "adapter_collect/int",
never@2895 125 "adapter_collect/long",
never@2895 126 "adapter_collect/float",
never@2895 127 "adapter_collect/double",
never@2895 128 "adapter_collect/void",
never@2895 129 "adapter_collect/0/ref",
never@2895 130 "adapter_collect/1/ref",
never@2895 131 "adapter_collect/2/ref",
never@2895 132 "adapter_collect/3/ref",
never@2895 133 "adapter_collect/4/ref",
never@2895 134 "adapter_collect/5/ref",
never@2895 135 "adapter_filter/S0/ref",
never@2895 136 "adapter_filter/S1/ref",
never@2895 137 "adapter_filter/S2/ref",
never@2895 138 "adapter_filter/S3/ref",
never@2895 139 "adapter_filter/S4/ref",
never@2895 140 "adapter_filter/S5/ref",
never@2895 141 "adapter_collect/2/S0/ref",
never@2895 142 "adapter_collect/2/S1/ref",
never@2895 143 "adapter_collect/2/S2/ref",
never@2895 144 "adapter_collect/2/S3/ref",
never@2895 145 "adapter_collect/2/S4/ref",
never@2895 146 "adapter_collect/2/S5/ref",
never@2895 147
never@2895 148 // blocking fold conversions:
never@2895 149 "adapter_fold/ref",
never@2895 150 "adapter_fold/int",
never@2895 151 "adapter_fold/long",
never@2895 152 "adapter_fold/float",
never@2895 153 "adapter_fold/double",
never@2895 154 "adapter_fold/void",
never@2895 155 "adapter_fold/1/ref",
never@2895 156 "adapter_fold/2/ref",
never@2895 157 "adapter_fold/3/ref",
never@2895 158 "adapter_fold/4/ref",
never@2895 159 "adapter_fold/5/ref",
jrose@1145 160
never@3105 161 "adapter_opt_profiling",
never@3105 162
jrose@1145 163 NULL
jrose@1145 164 };
jrose@1145 165
twisti@1734 166 // Adapters.
never@2950 167 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL;
twisti@1734 168
jrose@1474 169 jobject MethodHandles::_raise_exception_method;
jrose@1474 170
never@2895 171 address MethodHandles::_adapter_return_handlers[CONV_TYPE_MASK+1];
never@2895 172
jrose@1145 173 #ifdef ASSERT
jrose@1145 174 bool MethodHandles::spot_check_entry_names() {
jrose@1145 175 assert(!strcmp(entry_name(_invokestatic_mh), "invokestatic"), "");
jrose@1145 176 assert(!strcmp(entry_name(_bound_ref_mh), "bound_ref"), "");
jrose@1145 177 assert(!strcmp(entry_name(_adapter_retype_only), "adapter_retype_only"), "");
never@2895 178 assert(!strcmp(entry_name(_adapter_fold_args), "adapter_fold_args"), "");
jrose@1145 179 assert(!strcmp(entry_name(_adapter_opt_unboxi), "adapter_ref_to_prim/unboxi"), "");
never@2895 180 assert(!strcmp(entry_name(_adapter_opt_spread_char), "adapter_spread/char"), "");
never@2895 181 assert(!strcmp(entry_name(_adapter_opt_spread_double), "adapter_spread/double"), "");
never@2895 182 assert(!strcmp(entry_name(_adapter_opt_collect_int), "adapter_collect/int"), "");
never@2895 183 assert(!strcmp(entry_name(_adapter_opt_collect_0_ref), "adapter_collect/0/ref"), "");
never@2895 184 assert(!strcmp(entry_name(_adapter_opt_collect_2_S3_ref), "adapter_collect/2/S3/ref"), "");
never@2895 185 assert(!strcmp(entry_name(_adapter_opt_filter_S5_ref), "adapter_filter/S5/ref"), "");
never@2895 186 assert(!strcmp(entry_name(_adapter_opt_fold_3_ref), "adapter_fold/3/ref"), "");
never@2895 187 assert(!strcmp(entry_name(_adapter_opt_fold_void), "adapter_fold/void"), "");
jrose@1145 188 return true;
jrose@1145 189 }
jrose@1145 190 #endif
jrose@1145 191
twisti@1734 192
twisti@1734 193 //------------------------------------------------------------------------------
twisti@1734 194 // MethodHandles::generate_adapters
twisti@1734 195 //
twisti@2436 196 void MethodHandles::generate_adapters() {
never@2895 197 #ifdef TARGET_ARCH_NYI_6939861
never@2895 198 if (FLAG_IS_DEFAULT(UseRicochetFrames)) UseRicochetFrames = false;
never@2895 199 #endif
twisti@2698 200 if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return;
twisti@1734 201
twisti@1734 202 assert(_adapter_code == NULL, "generate only once");
twisti@1734 203
twisti@1734 204 ResourceMark rm;
twisti@1734 205 TraceTime timer("MethodHandles adapters generation", TraceStartupTime);
never@2950 206 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size);
twisti@1734 207 if (_adapter_code == NULL)
never@2950 208 vm_exit_out_of_memory(adapter_code_size, "CodeCache: no room for MethodHandles adapters");
never@3255 209 {
never@3255 210 CodeBuffer code(_adapter_code);
never@3255 211 MethodHandlesAdapterGenerator g(&code);
never@3255 212 g.generate();
never@3255 213 code.log_section_sizes("MethodHandlesAdapterBlob");
never@3255 214 }
twisti@1734 215 }
twisti@1734 216
twisti@1734 217 //------------------------------------------------------------------------------
twisti@1734 218 // MethodHandlesAdapterGenerator::generate
twisti@1734 219 //
twisti@2436 220 void MethodHandlesAdapterGenerator::generate() {
twisti@1734 221 // Generate generic method handle adapters.
twisti@1734 222 for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST;
twisti@1734 223 ek < MethodHandles::_EK_LIMIT;
twisti@1734 224 ek = MethodHandles::EntryKind(1 + (int)ek)) {
never@2895 225 if (MethodHandles::ek_supported(ek)) {
never@2895 226 StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek));
never@2895 227 MethodHandles::generate_method_handle_stub(_masm, ek);
never@2895 228 }
twisti@1734 229 }
twisti@1734 230 }
twisti@1734 231
twisti@1734 232
never@2895 233 #ifdef TARGET_ARCH_NYI_6939861
never@2895 234 // these defs belong in methodHandles_<arch>.cpp
never@2895 235 frame MethodHandles::ricochet_frame_sender(const frame& fr, RegisterMap *map) {
never@2895 236 ShouldNotCallThis();
never@2895 237 return fr;
never@2895 238 }
never@2895 239 void MethodHandles::ricochet_frame_oops_do(const frame& fr, OopClosure* f, const RegisterMap* reg_map) {
never@2895 240 ShouldNotCallThis();
never@2895 241 }
never@2895 242 #endif //TARGET_ARCH_NYI_6939861
never@2895 243
never@2895 244
never@2895 245 //------------------------------------------------------------------------------
never@2895 246 // MethodHandles::ek_supported
never@2895 247 //
never@2895 248 bool MethodHandles::ek_supported(MethodHandles::EntryKind ek) {
never@2895 249 MethodHandles::EntryKind ek_orig = MethodHandles::ek_original_kind(ek);
never@2895 250 switch (ek_orig) {
never@2895 251 case _adapter_unused_13:
never@2895 252 return false; // not defined yet
never@2895 253 case _adapter_prim_to_ref:
never@2895 254 return UseRicochetFrames && conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF);
never@2895 255 case _adapter_collect_args:
never@2895 256 return UseRicochetFrames && conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS);
never@2895 257 case _adapter_fold_args:
never@2895 258 return UseRicochetFrames && conv_op_supported(java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS);
never@2895 259 case _adapter_opt_return_any:
never@2895 260 return UseRicochetFrames;
never@2895 261 #ifdef TARGET_ARCH_NYI_6939861
never@2895 262 // ports before 6939861 supported only three kinds of spread ops
never@2895 263 case _adapter_spread_args:
never@2895 264 // restrict spreads to three kinds:
never@2895 265 switch (ek) {
never@2895 266 case _adapter_opt_spread_0:
never@2895 267 case _adapter_opt_spread_1:
never@2895 268 case _adapter_opt_spread_more:
never@2895 269 break;
never@2895 270 default:
never@2895 271 return false;
never@2895 272 break;
never@2895 273 }
never@2895 274 break;
never@2895 275 #endif //TARGET_ARCH_NYI_6939861
never@2895 276 }
never@2895 277 return true;
never@2895 278 }
never@2895 279
never@2895 280
jrose@1145 281 void MethodHandles::set_enabled(bool z) {
jrose@1145 282 if (_enabled != z) {
twisti@2698 283 guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic");
jrose@1145 284 _enabled = z;
jrose@1145 285 }
jrose@1145 286 }
jrose@1145 287
jrose@1145 288 // Note: A method which does not have a TRAPS argument cannot block in the GC
jrose@1145 289 // or throw exceptions. Such methods are used in this file to do something quick
jrose@1145 290 // and local, like parse a data structure. For speed, such methods work on plain
jrose@1145 291 // oops, not handles. Trapping methods uniformly operate on handles.
jrose@1145 292
twisti@2806 293 methodHandle MethodHandles::decode_vmtarget(oop vmtarget, int vmindex, oop mtype,
twisti@2806 294 KlassHandle& receiver_limit_result, int& decode_flags_result) {
twisti@2806 295 if (vmtarget == NULL) return methodHandle();
jrose@1145 296 assert(methodOopDesc::nonvirtual_vtable_index < 0, "encoding");
jrose@1145 297 if (vmindex < 0) {
jrose@1145 298 // this DMH performs no dispatch; it is directly bound to a methodOop
jrose@1145 299 // A MemberName may either be directly bound to a methodOop,
jrose@1145 300 // or it may use the klass/index form; both forms mean the same thing.
jrose@1145 301 methodOop m = decode_methodOop(methodOop(vmtarget), decode_flags_result);
jrose@1145 302 if ((decode_flags_result & _dmf_has_receiver) != 0
jrose@2639 303 && java_lang_invoke_MethodType::is_instance(mtype)) {
jrose@1145 304 // Extract receiver type restriction from mtype.ptypes[0].
jrose@2639 305 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype);
jrose@1145 306 oop ptype0 = (ptypes == NULL || ptypes->length() < 1) ? oop(NULL) : ptypes->obj_at(0);
jrose@1145 307 if (java_lang_Class::is_instance(ptype0))
jrose@1145 308 receiver_limit_result = java_lang_Class::as_klassOop(ptype0);
jrose@1145 309 }
jrose@1145 310 if (vmindex == methodOopDesc::nonvirtual_vtable_index) {
jrose@1145 311 // this DMH can be an "invokespecial" version
jrose@1145 312 decode_flags_result &= ~_dmf_does_dispatch;
jrose@1145 313 } else {
jrose@1145 314 assert(vmindex == methodOopDesc::invalid_vtable_index, "random vmindex?");
jrose@1145 315 }
jrose@1145 316 return m;
jrose@1145 317 } else {
twisti@1573 318 assert(vmtarget->is_klass(), "must be class or interface");
jrose@1145 319 decode_flags_result |= MethodHandles::_dmf_does_dispatch;
twisti@1573 320 decode_flags_result |= MethodHandles::_dmf_has_receiver;
jrose@1145 321 receiver_limit_result = (klassOop)vmtarget;
jrose@1145 322 Klass* tk = Klass::cast((klassOop)vmtarget);
jrose@1145 323 if (tk->is_interface()) {
jrose@1145 324 // an itable linkage is <interface, itable index>
jrose@1145 325 decode_flags_result |= MethodHandles::_dmf_from_interface;
jrose@1145 326 return klassItable::method_for_itable_index((klassOop)vmtarget, vmindex);
jrose@1145 327 } else {
jrose@1145 328 if (!tk->oop_is_instance())
never@1577 329 tk = instanceKlass::cast(SystemDictionary::Object_klass());
jrose@1145 330 return ((instanceKlass*)tk)->method_at_vtable(vmindex);
jrose@1145 331 }
jrose@1145 332 }
jrose@1145 333 }
jrose@1145 334
jrose@1145 335 // MemberName and DirectMethodHandle have the same linkage to the JVM internals.
jrose@1145 336 // (MemberName is the non-operational name used for queries and setup.)
jrose@1145 337
twisti@2806 338 methodHandle MethodHandles::decode_DirectMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) {
jrose@2639 339 oop vmtarget = java_lang_invoke_DirectMethodHandle::vmtarget(mh);
jrose@2639 340 int vmindex = java_lang_invoke_DirectMethodHandle::vmindex(mh);
jrose@2639 341 oop mtype = java_lang_invoke_DirectMethodHandle::type(mh);
jrose@1145 342 return decode_vmtarget(vmtarget, vmindex, mtype, receiver_limit_result, decode_flags_result);
jrose@1145 343 }
jrose@1145 344
twisti@2806 345 methodHandle MethodHandles::decode_BoundMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) {
jrose@2639 346 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh), "");
jrose@1474 347 assert(mh->klass() != SystemDictionary::AdapterMethodHandle_klass(), "");
jrose@1145 348 for (oop bmh = mh;;) {
jrose@1145 349 // Bound MHs can be stacked to bind several arguments.
jrose@2639 350 oop target = java_lang_invoke_MethodHandle::vmtarget(bmh);
twisti@2806 351 if (target == NULL) return methodHandle();
jrose@1145 352 decode_flags_result |= MethodHandles::_dmf_binds_argument;
jrose@1145 353 klassOop tk = target->klass();
jrose@1145 354 if (tk == SystemDictionary::BoundMethodHandle_klass()) {
jrose@1145 355 bmh = target;
jrose@1145 356 continue;
jrose@1145 357 } else {
jrose@2639 358 if (java_lang_invoke_MethodHandle::is_subclass(tk)) {
jrose@1145 359 //assert(tk == SystemDictionary::DirectMethodHandle_klass(), "end of BMH chain must be DMH");
jrose@1145 360 return decode_MethodHandle(target, receiver_limit_result, decode_flags_result);
jrose@1145 361 } else {
jrose@1145 362 // Optimized case: binding a receiver to a non-dispatched DMH
jrose@1145 363 // short-circuits directly to the methodOop.
jrose@1474 364 // (It might be another argument besides a receiver also.)
jrose@1145 365 assert(target->is_method(), "must be a simple method");
twisti@1573 366 decode_flags_result |= MethodHandles::_dmf_binds_method;
jrose@1145 367 methodOop m = (methodOop) target;
twisti@1573 368 if (!m->is_static())
twisti@1573 369 decode_flags_result |= MethodHandles::_dmf_has_receiver;
jrose@1145 370 return m;
jrose@1145 371 }
jrose@1145 372 }
jrose@1145 373 }
jrose@1145 374 }
jrose@1145 375
twisti@2806 376 methodHandle MethodHandles::decode_AdapterMethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) {
jrose@1145 377 assert(mh->klass() == SystemDictionary::AdapterMethodHandle_klass(), "");
jrose@1145 378 for (oop amh = mh;;) {
jrose@1145 379 // Adapter MHs can be stacked to convert several arguments.
jrose@2639 380 int conv_op = adapter_conversion_op(java_lang_invoke_AdapterMethodHandle::conversion(amh));
jrose@1145 381 decode_flags_result |= (_dmf_adapter_lsb << conv_op) & _DMF_ADAPTER_MASK;
jrose@2639 382 oop target = java_lang_invoke_MethodHandle::vmtarget(amh);
twisti@2806 383 if (target == NULL) return methodHandle();
jrose@1145 384 klassOop tk = target->klass();
jrose@1145 385 if (tk == SystemDictionary::AdapterMethodHandle_klass()) {
jrose@1145 386 amh = target;
jrose@1145 387 continue;
jrose@1145 388 } else {
jrose@1145 389 // must be a BMH (which will bind some more arguments) or a DMH (for the final call)
jrose@1145 390 return MethodHandles::decode_MethodHandle(target, receiver_limit_result, decode_flags_result);
jrose@1145 391 }
jrose@1145 392 }
jrose@1145 393 }
jrose@1145 394
twisti@2806 395 methodHandle MethodHandles::decode_MethodHandle(oop mh, KlassHandle& receiver_limit_result, int& decode_flags_result) {
twisti@2806 396 if (mh == NULL) return methodHandle();
jrose@1145 397 klassOop mhk = mh->klass();
jrose@2639 398 assert(java_lang_invoke_MethodHandle::is_subclass(mhk), "must be a MethodHandle");
jrose@1145 399 if (mhk == SystemDictionary::DirectMethodHandle_klass()) {
jrose@1145 400 return decode_DirectMethodHandle(mh, receiver_limit_result, decode_flags_result);
jrose@1145 401 } else if (mhk == SystemDictionary::BoundMethodHandle_klass()) {
jrose@1145 402 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result);
jrose@1145 403 } else if (mhk == SystemDictionary::AdapterMethodHandle_klass()) {
jrose@1145 404 return decode_AdapterMethodHandle(mh, receiver_limit_result, decode_flags_result);
jrose@2639 405 } else if (java_lang_invoke_BoundMethodHandle::is_subclass(mhk)) {
jrose@1474 406 // could be a JavaMethodHandle (but not an adapter MH)
jrose@1474 407 return decode_BoundMethodHandle(mh, receiver_limit_result, decode_flags_result);
jrose@1145 408 } else {
jrose@1145 409 assert(false, "cannot parse this MH");
twisti@2806 410 return methodHandle(); // random MH?
jrose@1145 411 }
jrose@1145 412 }
jrose@1145 413
jrose@1145 414 methodOop MethodHandles::decode_methodOop(methodOop m, int& decode_flags_result) {
jrose@1145 415 assert(m->is_method(), "");
jrose@1145 416 if (m->is_static()) {
jrose@1145 417 // check that signature begins '(L' or '([' (not '(I', '()', etc.)
coleenp@2497 418 Symbol* sig = m->signature();
jrose@1145 419 BasicType recv_bt = char2type(sig->byte_at(1));
jrose@1145 420 // Note: recv_bt might be T_ILLEGAL if byte_at(2) is ')'
jrose@1145 421 assert(sig->byte_at(0) == '(', "must be method sig");
twisti@1573 422 // if (recv_bt == T_OBJECT || recv_bt == T_ARRAY)
twisti@1573 423 // decode_flags_result |= _dmf_has_receiver;
jrose@1145 424 } else {
jrose@1145 425 // non-static method
jrose@1145 426 decode_flags_result |= _dmf_has_receiver;
jrose@1145 427 if (!m->can_be_statically_bound() && !m->is_initializer()) {
jrose@1145 428 decode_flags_result |= _dmf_does_dispatch;
jrose@1145 429 if (Klass::cast(m->method_holder())->is_interface())
jrose@1145 430 decode_flags_result |= _dmf_from_interface;
jrose@1145 431 }
jrose@1145 432 }
jrose@1145 433 return m;
jrose@1145 434 }
jrose@1145 435
jrose@1145 436
jrose@1145 437 // A trusted party is handing us a cookie to determine a method.
jrose@1145 438 // Let's boil it down to the method oop they really want.
twisti@2806 439 methodHandle MethodHandles::decode_method(oop x, KlassHandle& receiver_limit_result, int& decode_flags_result) {
jrose@1145 440 decode_flags_result = 0;
twisti@2806 441 receiver_limit_result = KlassHandle();
jrose@1145 442 klassOop xk = x->klass();
jrose@1145 443 if (xk == Universe::methodKlassObj()) {
jrose@1145 444 return decode_methodOop((methodOop) x, decode_flags_result);
jrose@1145 445 } else if (xk == SystemDictionary::MemberName_klass()) {
jrose@1145 446 // Note: This only works if the MemberName has already been resolved.
jrose@1145 447 return decode_MemberName(x, receiver_limit_result, decode_flags_result);
jrose@2639 448 } else if (java_lang_invoke_MethodHandle::is_subclass(xk)) {
jrose@1145 449 return decode_MethodHandle(x, receiver_limit_result, decode_flags_result);
never@1577 450 } else if (xk == SystemDictionary::reflect_Method_klass()) {
jrose@1145 451 oop clazz = java_lang_reflect_Method::clazz(x);
jrose@1145 452 int slot = java_lang_reflect_Method::slot(x);
jrose@1145 453 klassOop k = java_lang_Class::as_klassOop(clazz);
jrose@1145 454 if (k != NULL && Klass::cast(k)->oop_is_instance())
jrose@1145 455 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot),
jrose@1145 456 decode_flags_result);
never@1577 457 } else if (xk == SystemDictionary::reflect_Constructor_klass()) {
jrose@1145 458 oop clazz = java_lang_reflect_Constructor::clazz(x);
jrose@1145 459 int slot = java_lang_reflect_Constructor::slot(x);
jrose@1145 460 klassOop k = java_lang_Class::as_klassOop(clazz);
jrose@1145 461 if (k != NULL && Klass::cast(k)->oop_is_instance())
jrose@1145 462 return decode_methodOop(instanceKlass::cast(k)->method_with_idnum(slot),
jrose@1145 463 decode_flags_result);
jrose@1145 464 } else {
jrose@1145 465 // unrecognized object
jrose@1145 466 assert(!x->is_method(), "already checked");
jrose@2639 467 assert(!java_lang_invoke_MemberName::is_instance(x), "already checked");
jrose@1145 468 }
twisti@2806 469 return methodHandle();
jrose@1145 470 }
jrose@1145 471
jrose@1145 472
jrose@1145 473 int MethodHandles::decode_MethodHandle_stack_pushes(oop mh) {
jrose@1145 474 if (mh->klass() == SystemDictionary::DirectMethodHandle_klass())
jrose@1145 475 return 0; // no push/pop
jrose@2639 476 int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh);
jrose@1145 477 int last_vmslots = 0;
jrose@1145 478 oop last_mh = mh;
jrose@1145 479 for (;;) {
jrose@2639 480 oop target = java_lang_invoke_MethodHandle::vmtarget(last_mh);
jrose@1145 481 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) {
jrose@2639 482 last_vmslots = java_lang_invoke_MethodHandle::vmslots(target);
jrose@1145 483 break;
jrose@2639 484 } else if (!java_lang_invoke_MethodHandle::is_instance(target)) {
jrose@1145 485 // might be klass or method
jrose@1145 486 assert(target->is_method(), "must get here with a direct ref to method");
jrose@1145 487 last_vmslots = methodOop(target)->size_of_parameters();
jrose@1145 488 break;
jrose@1145 489 }
jrose@1145 490 last_mh = target;
jrose@1145 491 }
jrose@1145 492 // If I am called with fewer VM slots than my ultimate callee,
jrose@1145 493 // it must be that I push the additionally needed slots.
jrose@1145 494 // Likewise if am called with more VM slots, I will pop them.
jrose@1145 495 return (last_vmslots - this_vmslots);
jrose@1145 496 }
jrose@1145 497
jrose@1145 498
jrose@1145 499 // MemberName support
jrose@1145 500
jrose@2639 501 // import java_lang_invoke_MemberName.*
jrose@1145 502 enum {
jrose@2639 503 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD,
jrose@2639 504 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR,
jrose@2639 505 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD,
jrose@2639 506 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE,
jrose@2639 507 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES,
jrose@2639 508 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES,
jrose@1145 509 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE,
jrose@2639 510 VM_INDEX_UNINITIALIZED = java_lang_invoke_MemberName::VM_INDEX_UNINITIALIZED
jrose@1145 511 };
jrose@1145 512
jrose@1862 513 Handle MethodHandles::new_MemberName(TRAPS) {
jrose@1862 514 Handle empty;
jrose@1862 515 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass());
jrose@1862 516 if (!k->is_initialized()) k->initialize(CHECK_(empty));
jrose@1862 517 return Handle(THREAD, k->allocate_instance(THREAD));
jrose@1862 518 }
jrose@1862 519
jrose@1145 520 void MethodHandles::init_MemberName(oop mname_oop, oop target_oop) {
never@1577 521 if (target_oop->klass() == SystemDictionary::reflect_Field_klass()) {
jrose@1145 522 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder()
jrose@1145 523 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index()
jrose@1145 524 int mods = java_lang_reflect_Field::modifiers(target_oop);
jrose@1145 525 klassOop k = java_lang_Class::as_klassOop(clazz);
never@3137 526 int offset = instanceKlass::cast(k)->field_offset(slot);
jrose@1145 527 init_MemberName(mname_oop, k, accessFlags_from(mods), offset);
jrose@1145 528 } else {
twisti@2806 529 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 530 methodHandle m = MethodHandles::decode_method(target_oop, receiver_limit, decode_flags);
jrose@1145 531 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0);
twisti@2806 532 init_MemberName(mname_oop, m(), do_dispatch);
jrose@1145 533 }
jrose@1145 534 }
jrose@1145 535
jrose@1145 536 void MethodHandles::init_MemberName(oop mname_oop, methodOop m, bool do_dispatch) {
jrose@1145 537 int flags = ((m->is_initializer() ? IS_CONSTRUCTOR : IS_METHOD)
jrose@1145 538 | (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ));
jrose@1145 539 oop vmtarget = m;
jrose@1145 540 int vmindex = methodOopDesc::invalid_vtable_index; // implies no info yet
jrose@1145 541 if (!do_dispatch || (flags & IS_CONSTRUCTOR) || m->can_be_statically_bound())
jrose@1145 542 vmindex = methodOopDesc::nonvirtual_vtable_index; // implies never any dispatch
jrose@1145 543 assert(vmindex != VM_INDEX_UNINITIALIZED, "Java sentinel value");
jrose@2639 544 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget);
jrose@2639 545 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
jrose@2639 546 java_lang_invoke_MemberName::set_flags(mname_oop, flags);
jrose@2639 547 java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(m->method_holder())->java_mirror());
jrose@1145 548 }
jrose@1145 549
jrose@1145 550 void MethodHandles::init_MemberName(oop mname_oop, klassOop field_holder, AccessFlags mods, int offset) {
jrose@1145 551 int flags = (IS_FIELD | (jushort)( mods.as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ));
jrose@1145 552 oop vmtarget = field_holder;
jrose@1862 553 int vmindex = offset; // determines the field uniquely when combined with static bit
jrose@1145 554 assert(vmindex != VM_INDEX_UNINITIALIZED, "bad alias on vmindex");
jrose@2639 555 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget);
jrose@2639 556 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
jrose@2639 557 java_lang_invoke_MemberName::set_flags(mname_oop, flags);
jrose@2639 558 java_lang_invoke_MemberName::set_clazz(mname_oop, Klass::cast(field_holder)->java_mirror());
jrose@1145 559 }
jrose@1145 560
jrose@1145 561
twisti@2806 562 methodHandle MethodHandles::decode_MemberName(oop mname, KlassHandle& receiver_limit_result, int& decode_flags_result) {
twisti@2806 563 methodHandle empty;
jrose@2639 564 int flags = java_lang_invoke_MemberName::flags(mname);
twisti@2806 565 if ((flags & (IS_METHOD | IS_CONSTRUCTOR)) == 0) return empty; // not invocable
jrose@2639 566 oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname);
jrose@2639 567 int vmindex = java_lang_invoke_MemberName::vmindex(mname);
twisti@2806 568 if (vmindex == VM_INDEX_UNINITIALIZED) return empty; // not resolved
twisti@2806 569 methodHandle m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit_result, decode_flags_result);
jrose@2639 570 oop clazz = java_lang_invoke_MemberName::clazz(mname);
jrose@1474 571 if (clazz != NULL && java_lang_Class::is_instance(clazz)) {
jrose@1474 572 klassOop klass = java_lang_Class::as_klassOop(clazz);
jrose@1474 573 if (klass != NULL) receiver_limit_result = klass;
jrose@1474 574 }
jrose@1474 575 return m;
jrose@1145 576 }
jrose@1145 577
coleenp@2497 578 // convert the external string or reflective type to an internal signature
twisti@2806 579 Symbol* MethodHandles::convert_to_signature(oop type_str, bool polymorphic, TRAPS) {
jrose@2639 580 if (java_lang_invoke_MethodType::is_instance(type_str)) {
jrose@2639 581 return java_lang_invoke_MethodType::as_signature(type_str, polymorphic, CHECK_NULL);
coleenp@2497 582 } else if (java_lang_Class::is_instance(type_str)) {
coleenp@2497 583 return java_lang_Class::as_signature(type_str, false, CHECK_NULL);
coleenp@2497 584 } else if (java_lang_String::is_instance(type_str)) {
coleenp@2497 585 if (polymorphic) {
coleenp@2497 586 return java_lang_String::as_symbol(type_str, CHECK_NULL);
coleenp@2497 587 } else {
coleenp@2497 588 return java_lang_String::as_symbol_or_null(type_str);
coleenp@2497 589 }
coleenp@2497 590 } else {
coleenp@2497 591 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL);
coleenp@2497 592 }
coleenp@2497 593 }
coleenp@2497 594
jrose@1145 595 // An unresolved member name is a mere symbolic reference.
jrose@1145 596 // Resolving it plants a vmtarget/vmindex in it,
jrose@1145 597 // which refers dirctly to JVM internals.
jrose@1145 598 void MethodHandles::resolve_MemberName(Handle mname, TRAPS) {
jrose@2639 599 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
jrose@1145 600 #ifdef ASSERT
jrose@1145 601 // If this assert throws, renegotiate the sentinel value used by the Java code,
jrose@1145 602 // so that it is distinct from any valid vtable index value, and any special
jrose@1145 603 // values defined in methodOopDesc::VtableIndexFlag.
jrose@1145 604 // The point of the slop is to give the Java code and the JVM some room
jrose@1145 605 // to independently specify sentinel values.
jrose@1145 606 const int sentinel_slop = 10;
jrose@1145 607 const int sentinel_limit = methodOopDesc::highest_unused_vtable_index_value - sentinel_slop;
jrose@1145 608 assert(VM_INDEX_UNINITIALIZED < sentinel_limit, "Java sentinel != JVM sentinels");
jrose@1145 609 #endif
jrose@2639 610 if (java_lang_invoke_MemberName::vmindex(mname()) != VM_INDEX_UNINITIALIZED)
jrose@1145 611 return; // already resolved
twisti@2806 612 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname()));
twisti@2806 613 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname()));
twisti@2806 614 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname()));
twisti@2806 615 int flags = java_lang_invoke_MemberName::flags(mname());
jrose@1145 616
twisti@2806 617 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) {
jrose@1145 618 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve");
jrose@1145 619 }
twisti@2806 620
twisti@2806 621 instanceKlassHandle defc;
twisti@2806 622 {
twisti@2806 623 klassOop defc_klassOop = java_lang_Class::as_klassOop(defc_oop());
twisti@2806 624 if (defc_klassOop == NULL) return; // a primitive; no resolution possible
twisti@2806 625 if (!Klass::cast(defc_klassOop)->oop_is_instance()) {
twisti@2806 626 if (!Klass::cast(defc_klassOop)->oop_is_array()) return;
twisti@2806 627 defc_klassOop = SystemDictionary::Object_klass();
twisti@2806 628 }
twisti@2806 629 defc = instanceKlassHandle(THREAD, defc_klassOop);
jrose@1145 630 }
jrose@1145 631 if (defc.is_null()) {
jrose@1145 632 THROW_MSG(vmSymbols::java_lang_InternalError(), "primitive class");
jrose@1145 633 }
twisti@2806 634 defc->link_class(CHECK); // possible safepoint
jrose@1145 635
jrose@1145 636 // convert the external string name to an internal symbol
twisti@2806 637 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str());
coleenp@2497 638 if (name == NULL) return; // no such name
never@2978 639 if (name == vmSymbols::class_initializer_name())
never@2978 640 return; // illegal name
jrose@1145 641
jrose@1863 642 Handle polymorphic_method_type;
jrose@1863 643 bool polymorphic_signature = false;
jrose@1863 644 if ((flags & ALL_KINDS) == IS_METHOD &&
twisti@2343 645 (defc() == SystemDictionary::MethodHandle_klass() &&
twisti@2806 646 methodOopDesc::is_method_handle_invoke_name(name))) {
jrose@1863 647 polymorphic_signature = true;
twisti@2806 648 }
jrose@1863 649
jrose@1145 650 // convert the external string or reflective type to an internal signature
twisti@2806 651 TempNewSymbol type = convert_to_signature(type_str(), polymorphic_signature, CHECK);
twisti@2806 652 if (java_lang_invoke_MethodType::is_instance(type_str()) && polymorphic_signature) {
twisti@2806 653 polymorphic_method_type = type_str; // preserve exactly
jrose@1145 654 }
coleenp@2497 655 if (type == NULL) return; // no such signature exists in the VM
jrose@1145 656
jrose@1145 657 // Time to do the lookup.
jrose@1145 658 switch (flags & ALL_KINDS) {
jrose@1145 659 case IS_METHOD:
jrose@1145 660 {
jrose@1145 661 CallInfo result;
jrose@1145 662 {
jrose@1145 663 EXCEPTION_MARK;
jrose@1145 664 if ((flags & JVM_ACC_STATIC) != 0) {
jrose@1145 665 LinkResolver::resolve_static_call(result,
jrose@1145 666 defc, name, type, KlassHandle(), false, false, THREAD);
jrose@1145 667 } else if (defc->is_interface()) {
jrose@1145 668 LinkResolver::resolve_interface_call(result, Handle(), defc,
jrose@1145 669 defc, name, type, KlassHandle(), false, false, THREAD);
jrose@1145 670 } else {
jrose@1145 671 LinkResolver::resolve_virtual_call(result, Handle(), defc,
jrose@1145 672 defc, name, type, KlassHandle(), false, false, THREAD);
jrose@1145 673 }
jrose@1145 674 if (HAS_PENDING_EXCEPTION) {
jrose@1145 675 CLEAR_PENDING_EXCEPTION;
jrose@1863 676 break; // go to second chance
jrose@1145 677 }
jrose@1145 678 }
jrose@1145 679 methodHandle m = result.resolved_method();
jrose@1145 680 oop vmtarget = NULL;
jrose@1145 681 int vmindex = methodOopDesc::nonvirtual_vtable_index;
jrose@1145 682 if (defc->is_interface()) {
jrose@1145 683 vmindex = klassItable::compute_itable_index(m());
jrose@1145 684 assert(vmindex >= 0, "");
jrose@1145 685 } else if (result.has_vtable_index()) {
jrose@1145 686 vmindex = result.vtable_index();
jrose@1145 687 assert(vmindex >= 0, "");
jrose@1145 688 }
jrose@1145 689 assert(vmindex != VM_INDEX_UNINITIALIZED, "");
jrose@1145 690 if (vmindex < 0) {
jrose@1145 691 assert(result.is_statically_bound(), "");
jrose@1145 692 vmtarget = m();
jrose@1145 693 } else {
jrose@1145 694 vmtarget = result.resolved_klass()->as_klassOop();
jrose@1145 695 }
jrose@1145 696 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
jrose@2639 697 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget);
jrose@2639 698 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex);
jrose@2639 699 java_lang_invoke_MemberName::set_modifiers(mname(), mods);
twisti@2806 700 DEBUG_ONLY(KlassHandle junk1; int junk2);
twisti@2806 701 assert(decode_MemberName(mname(), junk1, junk2) == result.resolved_method(),
jrose@1145 702 "properly stored for later decoding");
jrose@1145 703 return;
jrose@1145 704 }
jrose@1145 705 case IS_CONSTRUCTOR:
jrose@1145 706 {
jrose@1145 707 CallInfo result;
jrose@1145 708 {
jrose@1145 709 EXCEPTION_MARK;
coleenp@2497 710 if (name == vmSymbols::object_initializer_name()) {
jrose@1145 711 LinkResolver::resolve_special_call(result,
jrose@1145 712 defc, name, type, KlassHandle(), false, THREAD);
jrose@1145 713 } else {
jrose@1145 714 break; // will throw after end of switch
jrose@1145 715 }
jrose@1145 716 if (HAS_PENDING_EXCEPTION) {
jrose@1145 717 CLEAR_PENDING_EXCEPTION;
jrose@1145 718 return;
jrose@1145 719 }
jrose@1145 720 }
jrose@1145 721 assert(result.is_statically_bound(), "");
jrose@1145 722 methodHandle m = result.resolved_method();
jrose@1145 723 oop vmtarget = m();
jrose@1145 724 int vmindex = methodOopDesc::nonvirtual_vtable_index;
jrose@1145 725 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
jrose@2639 726 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget);
jrose@2639 727 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex);
jrose@2639 728 java_lang_invoke_MemberName::set_modifiers(mname(), mods);
twisti@2806 729 DEBUG_ONLY(KlassHandle junk1; int junk2);
twisti@2806 730 assert(decode_MemberName(mname(), junk1, junk2) == result.resolved_method(),
jrose@1145 731 "properly stored for later decoding");
jrose@1145 732 return;
jrose@1145 733 }
jrose@1145 734 case IS_FIELD:
jrose@1145 735 {
jrose@1145 736 // This is taken from LinkResolver::resolve_field, sans access checks.
jrose@1145 737 fieldDescriptor fd; // find_field initializes fd if found
coleenp@2497 738 KlassHandle sel_klass(THREAD, instanceKlass::cast(defc())->find_field(name, type, &fd));
jrose@1145 739 // check if field exists; i.e., if a klass containing the field def has been selected
jrose@1145 740 if (sel_klass.is_null()) return;
jrose@1145 741 oop vmtarget = sel_klass->as_klassOop();
jrose@1145 742 int vmindex = fd.offset();
jrose@1145 743 int mods = (fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS);
jrose@1145 744 if (vmindex == VM_INDEX_UNINITIALIZED) break; // should not happen
jrose@2639 745 java_lang_invoke_MemberName::set_vmtarget(mname(), vmtarget);
jrose@2639 746 java_lang_invoke_MemberName::set_vmindex(mname(), vmindex);
jrose@2639 747 java_lang_invoke_MemberName::set_modifiers(mname(), mods);
jrose@1145 748 return;
jrose@1145 749 }
jrose@1863 750 default:
jrose@1863 751 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
jrose@1145 752 }
jrose@1863 753
jrose@1863 754 // Second chance.
jrose@1863 755 if (polymorphic_method_type.not_null()) {
jrose@1863 756 // Look on a non-null class loader.
jrose@1863 757 Handle cur_class_loader;
jrose@2639 758 const int nptypes = java_lang_invoke_MethodType::ptype_count(polymorphic_method_type());
jrose@1863 759 for (int i = 0; i <= nptypes; i++) {
jrose@1863 760 oop type_mirror;
jrose@2639 761 if (i < nptypes) type_mirror = java_lang_invoke_MethodType::ptype(polymorphic_method_type(), i);
jrose@2639 762 else type_mirror = java_lang_invoke_MethodType::rtype(polymorphic_method_type());
jrose@1863 763 klassOop example_type = java_lang_Class::as_klassOop(type_mirror);
jrose@1863 764 if (example_type == NULL) continue;
jrose@1863 765 oop class_loader = Klass::cast(example_type)->class_loader();
jrose@1863 766 if (class_loader == NULL || class_loader == cur_class_loader()) continue;
jrose@1863 767 cur_class_loader = Handle(THREAD, class_loader);
jrose@1863 768 methodOop m = SystemDictionary::find_method_handle_invoke(name,
jrose@1863 769 type,
jrose@1863 770 KlassHandle(THREAD, example_type),
jrose@1863 771 THREAD);
jrose@1863 772 if (HAS_PENDING_EXCEPTION) {
jrose@1863 773 CLEAR_PENDING_EXCEPTION;
jrose@1863 774 m = NULL;
jrose@1863 775 // try again with a different class loader...
jrose@1863 776 }
jrose@2982 777 if (m != NULL &&
jrose@2982 778 m->is_method_handle_invoke() &&
jrose@2982 779 java_lang_invoke_MethodType::equals(polymorphic_method_type(), m->method_handle_type())) {
jrose@1863 780 int mods = (m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS);
jrose@2639 781 java_lang_invoke_MemberName::set_vmtarget(mname(), m);
jrose@2639 782 java_lang_invoke_MemberName::set_vmindex(mname(), m->vtable_index());
jrose@2639 783 java_lang_invoke_MemberName::set_modifiers(mname(), mods);
jrose@1863 784 return;
jrose@1863 785 }
jrose@1863 786 }
jrose@1863 787 }
jrose@1145 788 }
jrose@1145 789
jrose@1145 790 // Conversely, a member name which is only initialized from JVM internals
jrose@1145 791 // may have null defc, name, and type fields.
jrose@1145 792 // Resolving it plants a vmtarget/vmindex in it,
jrose@1145 793 // which refers directly to JVM internals.
jrose@1145 794 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
jrose@2639 795 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
jrose@2639 796 oop vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
jrose@2639 797 int vmindex = java_lang_invoke_MemberName::vmindex(mname());
jrose@1145 798 if (vmtarget == NULL || vmindex == VM_INDEX_UNINITIALIZED) {
jrose@1145 799 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
jrose@1145 800 }
jrose@1145 801
jrose@2639 802 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
jrose@2639 803 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
jrose@2639 804 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
jrose@2639 805 int flags = java_lang_invoke_MemberName::flags(mname());
jrose@1145 806
jrose@1145 807 if (suppress != 0) {
jrose@1145 808 if (suppress & _suppress_defc) have_defc = true;
jrose@1145 809 if (suppress & _suppress_name) have_name = true;
jrose@1145 810 if (suppress & _suppress_type) have_type = true;
jrose@1145 811 }
jrose@1145 812
jrose@1145 813 if (have_defc && have_name && have_type) return; // nothing needed
jrose@1145 814
jrose@1145 815 switch (flags & ALL_KINDS) {
jrose@1145 816 case IS_METHOD:
jrose@1145 817 case IS_CONSTRUCTOR:
jrose@1145 818 {
twisti@2806 819 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 820 methodHandle m = decode_vmtarget(vmtarget, vmindex, NULL, receiver_limit, decode_flags);
jrose@1145 821 if (m.is_null()) break;
jrose@1145 822 if (!have_defc) {
jrose@1145 823 klassOop defc = m->method_holder();
twisti@2806 824 if (receiver_limit.not_null() && receiver_limit() != defc
twisti@2806 825 && Klass::cast(receiver_limit())->is_subtype_of(defc))
twisti@2806 826 defc = receiver_limit();
jrose@2639 827 java_lang_invoke_MemberName::set_clazz(mname(), Klass::cast(defc)->java_mirror());
jrose@1145 828 }
jrose@1145 829 if (!have_name) {
jrose@1145 830 //not java_lang_String::create_from_symbol; let's intern member names
jrose@1145 831 Handle name = StringTable::intern(m->name(), CHECK);
jrose@2639 832 java_lang_invoke_MemberName::set_name(mname(), name());
jrose@1145 833 }
jrose@1145 834 if (!have_type) {
jrose@1145 835 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
jrose@2639 836 java_lang_invoke_MemberName::set_type(mname(), type());
jrose@1145 837 }
jrose@1145 838 return;
jrose@1145 839 }
jrose@1145 840 case IS_FIELD:
jrose@1145 841 {
jrose@1145 842 // This is taken from LinkResolver::resolve_field, sans access checks.
jrose@1145 843 if (!vmtarget->is_klass()) break;
jrose@1145 844 if (!Klass::cast((klassOop) vmtarget)->oop_is_instance()) break;
jrose@1145 845 instanceKlassHandle defc(THREAD, (klassOop) vmtarget);
jrose@1145 846 bool is_static = ((flags & JVM_ACC_STATIC) != 0);
jrose@1145 847 fieldDescriptor fd; // find_field initializes fd if found
jrose@1145 848 if (!defc->find_field_from_offset(vmindex, is_static, &fd))
jrose@1145 849 break; // cannot expand
jrose@1145 850 if (!have_defc) {
jrose@2639 851 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
jrose@1145 852 }
jrose@1145 853 if (!have_name) {
jrose@1145 854 //not java_lang_String::create_from_symbol; let's intern member names
jrose@1145 855 Handle name = StringTable::intern(fd.name(), CHECK);
jrose@2639 856 java_lang_invoke_MemberName::set_name(mname(), name());
jrose@1145 857 }
jrose@1145 858 if (!have_type) {
jrose@1145 859 Handle type = java_lang_String::create_from_symbol(fd.signature(), CHECK);
jrose@2639 860 java_lang_invoke_MemberName::set_type(mname(), type());
jrose@1145 861 }
jrose@1145 862 return;
jrose@1145 863 }
jrose@1145 864 }
jrose@1145 865 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
jrose@1145 866 }
jrose@1145 867
jrose@1145 868 int MethodHandles::find_MemberNames(klassOop k,
coleenp@2497 869 Symbol* name, Symbol* sig,
jrose@1145 870 int mflags, klassOop caller,
jrose@1145 871 int skip, objArrayOop results) {
jrose@1145 872 DEBUG_ONLY(No_Safepoint_Verifier nsv);
jrose@1145 873 // this code contains no safepoints!
jrose@1145 874
jrose@1145 875 // %%% take caller into account!
jrose@1145 876
jrose@1145 877 if (k == NULL || !Klass::cast(k)->oop_is_instance()) return -1;
jrose@1145 878
jrose@1145 879 int rfill = 0, rlimit = results->length(), rskip = skip;
jrose@1145 880 // overflow measurement:
jrose@1145 881 int overflow = 0, overflow_limit = MAX2(1000, rlimit);
jrose@1145 882
jrose@1145 883 int match_flags = mflags;
jrose@1145 884 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
jrose@1145 885 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0);
jrose@1145 886 bool local_only = !(search_superc | search_intfc);
jrose@1145 887 bool classes_only = false;
jrose@1145 888
jrose@1145 889 if (name != NULL) {
jrose@1145 890 if (name->utf8_length() == 0) return 0; // a match is not possible
jrose@1145 891 }
jrose@1145 892 if (sig != NULL) {
jrose@1145 893 if (sig->utf8_length() == 0) return 0; // a match is not possible
jrose@1145 894 if (sig->byte_at(0) == '(')
jrose@1145 895 match_flags &= ~(IS_FIELD | IS_TYPE);
jrose@1145 896 else
jrose@1145 897 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
jrose@1145 898 }
jrose@1145 899
jrose@1145 900 if ((match_flags & IS_TYPE) != 0) {
jrose@1145 901 // NYI, and Core Reflection works quite well for this query
jrose@1145 902 }
jrose@1145 903
jrose@1145 904 if ((match_flags & IS_FIELD) != 0) {
jrose@1145 905 for (FieldStream st(k, local_only, !search_intfc); !st.eos(); st.next()) {
jrose@1145 906 if (name != NULL && st.name() != name)
jrose@1145 907 continue;
jrose@1145 908 if (sig != NULL && st.signature() != sig)
jrose@1145 909 continue;
jrose@1145 910 // passed the filters
jrose@1145 911 if (rskip > 0) {
jrose@1145 912 --rskip;
jrose@1145 913 } else if (rfill < rlimit) {
jrose@1145 914 oop result = results->obj_at(rfill++);
jrose@2639 915 if (!java_lang_invoke_MemberName::is_instance(result))
jrose@1145 916 return -99; // caller bug!
jrose@1145 917 MethodHandles::init_MemberName(result, st.klass()->as_klassOop(), st.access_flags(), st.offset());
jrose@1145 918 } else if (++overflow >= overflow_limit) {
jrose@1145 919 match_flags = 0; break; // got tired of looking at overflow
jrose@1145 920 }
jrose@1145 921 }
jrose@1145 922 }
jrose@1145 923
jrose@1145 924 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
jrose@1145 925 // watch out for these guys:
coleenp@2497 926 Symbol* init_name = vmSymbols::object_initializer_name();
coleenp@2497 927 Symbol* clinit_name = vmSymbols::class_initializer_name();
jrose@1145 928 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit>
jrose@1145 929 bool negate_name_test = false;
jrose@1145 930 // fix name so that it captures the intention of IS_CONSTRUCTOR
jrose@1145 931 if (!(match_flags & IS_METHOD)) {
jrose@1145 932 // constructors only
jrose@1145 933 if (name == NULL) {
jrose@1145 934 name = init_name;
jrose@1145 935 } else if (name != init_name) {
jrose@1145 936 return 0; // no constructors of this method name
jrose@1145 937 }
jrose@1145 938 } else if (!(match_flags & IS_CONSTRUCTOR)) {
jrose@1145 939 // methods only
jrose@1145 940 if (name == NULL) {
jrose@1145 941 name = init_name;
jrose@1145 942 negate_name_test = true; // if we see the name, we *omit* the entry
jrose@1145 943 } else if (name == init_name) {
jrose@1145 944 return 0; // no methods of this constructor name
jrose@1145 945 }
jrose@1145 946 } else {
jrose@1145 947 // caller will accept either sort; no need to adjust name
jrose@1145 948 }
jrose@1145 949 for (MethodStream st(k, local_only, !search_intfc); !st.eos(); st.next()) {
jrose@1145 950 methodOop m = st.method();
coleenp@2497 951 Symbol* m_name = m->name();
jrose@1145 952 if (m_name == clinit_name)
jrose@1145 953 continue;
jrose@1145 954 if (name != NULL && ((m_name != name) ^ negate_name_test))
jrose@1145 955 continue;
jrose@1145 956 if (sig != NULL && m->signature() != sig)
jrose@1145 957 continue;
jrose@1145 958 // passed the filters
jrose@1145 959 if (rskip > 0) {
jrose@1145 960 --rskip;
jrose@1145 961 } else if (rfill < rlimit) {
jrose@1145 962 oop result = results->obj_at(rfill++);
jrose@2639 963 if (!java_lang_invoke_MemberName::is_instance(result))
jrose@1145 964 return -99; // caller bug!
jrose@1145 965 MethodHandles::init_MemberName(result, m, true);
jrose@1145 966 } else if (++overflow >= overflow_limit) {
jrose@1145 967 match_flags = 0; break; // got tired of looking at overflow
jrose@1145 968 }
jrose@1145 969 }
jrose@1145 970 }
jrose@1145 971
jrose@1145 972 // return number of elements we at leasted wanted to initialize
jrose@1145 973 return rfill + overflow;
jrose@1145 974 }
jrose@1145 975
jrose@1145 976
jrose@1862 977 // Decode this java.lang.Class object into an instanceKlass, if possible.
jrose@1862 978 // Throw IAE if not
jrose@1862 979 instanceKlassHandle MethodHandles::resolve_instance_klass(oop java_mirror_oop, TRAPS) {
jrose@1862 980 instanceKlassHandle empty;
jrose@1862 981 klassOop caller = NULL;
jrose@1862 982 if (java_lang_Class::is_instance(java_mirror_oop)) {
jrose@1862 983 caller = java_lang_Class::as_klassOop(java_mirror_oop);
jrose@1862 984 }
jrose@1862 985 if (caller == NULL || !Klass::cast(caller)->oop_is_instance()) {
jrose@1862 986 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "not a class", empty);
jrose@1862 987 }
jrose@1862 988 return instanceKlassHandle(THREAD, caller);
jrose@1862 989 }
jrose@1862 990
jrose@1145 991
jrose@1145 992
jrose@1145 993 // Decode the vmtarget field of a method handle.
jrose@1145 994 // Sanitize out methodOops, klassOops, and any other non-Java data.
jrose@1145 995 // This is for debugging and reflection.
jrose@1145 996 oop MethodHandles::encode_target(Handle mh, int format, TRAPS) {
jrose@2639 997 assert(java_lang_invoke_MethodHandle::is_instance(mh()), "must be a MH");
jrose@2982 998 if (format == ETF_FORCE_DIRECT_HANDLE ||
jrose@2982 999 format == ETF_COMPILE_DIRECT_HANDLE) {
jrose@2982 1000 // Internal function for stress testing.
jrose@2982 1001 Handle mt = java_lang_invoke_MethodHandle::type(mh());
jrose@2982 1002 int invocation_count = 10000;
jrose@2982 1003 TempNewSymbol signature = java_lang_invoke_MethodType::as_signature(mt(), true, CHECK_NULL);
jrose@2982 1004 bool omit_receiver_argument = true;
jrose@2982 1005 MethodHandleCompiler mhc(mh, vmSymbols::invoke_name(), signature, invocation_count, omit_receiver_argument, CHECK_NULL);
jrose@2982 1006 methodHandle m = mhc.compile(CHECK_NULL);
jrose@2982 1007 if (StressMethodHandleWalk && Verbose || PrintMiscellaneous) {
jrose@2982 1008 tty->print_cr("MethodHandleNatives.getTarget(%s)",
jrose@2982 1009 format == ETF_FORCE_DIRECT_HANDLE ? "FORCE_DIRECT" : "COMPILE_DIRECT");
jrose@2982 1010 if (Verbose) {
jrose@2982 1011 m->print_codes();
jrose@2982 1012 }
jrose@2982 1013 }
jrose@2982 1014 if (StressMethodHandleWalk) {
jrose@2982 1015 InterpreterOopMap mask;
jrose@2982 1016 OopMapCache::compute_one_oop_map(m, m->code_size() - 1, &mask);
jrose@2982 1017 }
jrose@2982 1018 if ((format == ETF_COMPILE_DIRECT_HANDLE ||
jrose@2982 1019 CompilationPolicy::must_be_compiled(m))
jrose@2982 1020 && !instanceKlass::cast(m->method_holder())->is_not_initialized()
jrose@2982 1021 && CompilationPolicy::can_be_compiled(m)) {
jrose@2982 1022 // Force compilation
jrose@2982 1023 CompileBroker::compile_method(m, InvocationEntryBci,
iveresov@3035 1024 CompilationPolicy::policy()->initial_compile_level(),
jrose@2982 1025 methodHandle(), 0, "MethodHandleNatives.getTarget",
jrose@2982 1026 CHECK_NULL);
jrose@2982 1027 }
jrose@2982 1028 // Now wrap m in a DirectMethodHandle.
jrose@2982 1029 instanceKlassHandle dmh_klass(THREAD, SystemDictionary::DirectMethodHandle_klass());
jrose@2982 1030 Handle dmh = dmh_klass->allocate_instance_handle(CHECK_NULL);
jrose@2982 1031 JavaValue ignore_result(T_VOID);
jrose@2982 1032 Symbol* init_name = vmSymbols::object_initializer_name();
jrose@2982 1033 Symbol* init_sig = vmSymbols::notifyGenericMethodType_signature();
jrose@2982 1034 JavaCalls::call_special(&ignore_result, dmh,
jrose@2982 1035 SystemDictionaryHandles::MethodHandle_klass(), init_name, init_sig,
jrose@2982 1036 java_lang_invoke_MethodHandle::type(mh()), CHECK_NULL);
jrose@2982 1037 MethodHandles::init_DirectMethodHandle(dmh, m, false, CHECK_NULL);
jrose@2982 1038 return dmh();
jrose@2982 1039 }
jrose@1145 1040 if (format == ETF_HANDLE_OR_METHOD_NAME) {
jrose@2639 1041 oop target = java_lang_invoke_MethodHandle::vmtarget(mh());
jrose@1145 1042 if (target == NULL) {
jrose@1145 1043 return NULL; // unformed MH
jrose@1145 1044 }
jrose@1145 1045 klassOop tklass = target->klass();
never@1577 1046 if (Klass::cast(tklass)->is_subclass_of(SystemDictionary::Object_klass())) {
jrose@1145 1047 return target; // target is another MH (or something else?)
jrose@1145 1048 }
jrose@1145 1049 }
jrose@1145 1050 if (format == ETF_DIRECT_HANDLE) {
jrose@1145 1051 oop target = mh();
jrose@1145 1052 for (;;) {
jrose@1145 1053 if (target->klass() == SystemDictionary::DirectMethodHandle_klass()) {
jrose@1145 1054 return target;
jrose@1145 1055 }
jrose@2639 1056 if (!java_lang_invoke_MethodHandle::is_instance(target)){
jrose@1145 1057 return NULL; // unformed MH
jrose@1145 1058 }
jrose@2639 1059 target = java_lang_invoke_MethodHandle::vmtarget(target);
jrose@1145 1060 }
jrose@1145 1061 }
jrose@1145 1062 // cases of metadata in MH.vmtarget:
jrose@1145 1063 // - AMH can have methodOop for static invoke with bound receiver
jrose@1145 1064 // - DMH can have methodOop for static invoke (on variable receiver)
jrose@1145 1065 // - DMH can have klassOop for dispatched (non-static) invoke
twisti@2806 1066 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 1067 methodHandle m = decode_MethodHandle(mh(), receiver_limit, decode_flags);
twisti@2806 1068 if (m.is_null()) return NULL;
jrose@1145 1069 switch (format) {
jrose@1145 1070 case ETF_REFLECT_METHOD:
jrose@1145 1071 // same as jni_ToReflectedMethod:
jrose@1145 1072 if (m->is_initializer()) {
jrose@1145 1073 return Reflection::new_constructor(m, THREAD);
jrose@1145 1074 } else {
jrose@1145 1075 return Reflection::new_method(m, UseNewReflection, false, THREAD);
jrose@1145 1076 }
jrose@1145 1077
jrose@1145 1078 case ETF_HANDLE_OR_METHOD_NAME: // method, not handle
jrose@1145 1079 case ETF_METHOD_NAME:
jrose@1145 1080 {
jrose@1145 1081 if (SystemDictionary::MemberName_klass() == NULL) break;
jrose@1145 1082 instanceKlassHandle mname_klass(THREAD, SystemDictionary::MemberName_klass());
jrose@1145 1083 mname_klass->initialize(CHECK_NULL);
twisti@2806 1084 Handle mname = mname_klass->allocate_instance_handle(CHECK_NULL); // possible safepoint
jrose@2639 1085 java_lang_invoke_MemberName::set_vmindex(mname(), VM_INDEX_UNINITIALIZED);
jrose@1145 1086 bool do_dispatch = ((decode_flags & MethodHandles::_dmf_does_dispatch) != 0);
twisti@2806 1087 init_MemberName(mname(), m(), do_dispatch);
jrose@1145 1088 expand_MemberName(mname, 0, CHECK_NULL);
jrose@1145 1089 return mname();
jrose@1145 1090 }
jrose@1145 1091 }
jrose@1145 1092
jrose@1145 1093 // Unknown format code.
jrose@1145 1094 char msg[50];
jrose@1145 1095 jio_snprintf(msg, sizeof(msg), "unknown getTarget format=%d", format);
jrose@1145 1096 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), msg);
jrose@1145 1097 }
jrose@1145 1098
jrose@1474 1099 static const char* always_null_names[] = {
jrose@1474 1100 "java/lang/Void",
jrose@1474 1101 "java/lang/Null",
jrose@1474 1102 //"java/lang/Nothing",
jrose@1474 1103 "sun/dyn/empty/Empty",
jrose@2639 1104 "sun/invoke/empty/Empty",
jrose@1474 1105 NULL
jrose@1474 1106 };
jrose@1474 1107
jrose@1474 1108 static bool is_always_null_type(klassOop klass) {
jrose@2760 1109 if (klass == NULL) return false; // safety
jrose@1474 1110 if (!Klass::cast(klass)->oop_is_instance()) return false;
jrose@1474 1111 instanceKlass* ik = instanceKlass::cast(klass);
jrose@1474 1112 // Must be on the boot class path:
jrose@1474 1113 if (ik->class_loader() != NULL) return false;
jrose@1474 1114 // Check the name.
coleenp@2497 1115 Symbol* name = ik->name();
jrose@1474 1116 for (int i = 0; ; i++) {
jrose@1474 1117 const char* test_name = always_null_names[i];
jrose@1474 1118 if (test_name == NULL) break;
twisti@1573 1119 if (name->equals(test_name))
jrose@1474 1120 return true;
jrose@1474 1121 }
jrose@1474 1122 return false;
jrose@1474 1123 }
jrose@1474 1124
jrose@1145 1125 bool MethodHandles::class_cast_needed(klassOop src, klassOop dst) {
jrose@2760 1126 if (dst == NULL) return true;
jrose@2760 1127 if (src == NULL) return (dst != SystemDictionary::Object_klass());
never@1577 1128 if (src == dst || dst == SystemDictionary::Object_klass())
jrose@1145 1129 return false; // quickest checks
jrose@1145 1130 Klass* srck = Klass::cast(src);
jrose@1145 1131 Klass* dstk = Klass::cast(dst);
jrose@1145 1132 if (dstk->is_interface()) {
jrose@1145 1133 // interface receivers can safely be viewed as untyped,
jrose@1145 1134 // because interface calls always include a dynamic check
never@1577 1135 //dstk = Klass::cast(SystemDictionary::Object_klass());
jrose@1145 1136 return false;
jrose@1145 1137 }
jrose@1145 1138 if (srck->is_interface()) {
jrose@1145 1139 // interface arguments must be viewed as untyped
never@1577 1140 //srck = Klass::cast(SystemDictionary::Object_klass());
jrose@1145 1141 return true;
jrose@1145 1142 }
jrose@1474 1143 if (is_always_null_type(src)) {
jrose@1474 1144 // some source types are known to be never instantiated;
jrose@1474 1145 // they represent references which are always null
jrose@1474 1146 // such null references never fail to convert safely
jrose@1474 1147 return false;
jrose@1474 1148 }
jrose@1145 1149 return !srck->is_subclass_of(dstk->as_klassOop());
jrose@1145 1150 }
jrose@1145 1151
jrose@1145 1152 static oop object_java_mirror() {
never@1577 1153 return Klass::cast(SystemDictionary::Object_klass())->java_mirror();
jrose@1145 1154 }
jrose@1145 1155
twisti@2903 1156 bool MethodHandles::is_float_fixed_reinterpretation_cast(BasicType src, BasicType dst) {
twisti@2903 1157 if (src == T_FLOAT) return dst == T_INT;
twisti@2903 1158 if (src == T_INT) return dst == T_FLOAT;
twisti@2903 1159 if (src == T_DOUBLE) return dst == T_LONG;
twisti@2903 1160 if (src == T_LONG) return dst == T_DOUBLE;
twisti@2903 1161 return false;
twisti@2903 1162 }
twisti@2903 1163
jrose@1145 1164 bool MethodHandles::same_basic_type_for_arguments(BasicType src,
jrose@1145 1165 BasicType dst,
jrose@1474 1166 bool raw,
jrose@1145 1167 bool for_return) {
jrose@1474 1168 if (for_return) {
jrose@1474 1169 // return values can always be forgotten:
jrose@1474 1170 if (dst == T_VOID) return true;
jrose@1474 1171 if (src == T_VOID) return raw && (dst == T_INT);
jrose@1474 1172 // We allow caller to receive a garbage int, which is harmless.
jrose@1474 1173 // This trick is pulled by trusted code (see VerifyType.canPassRaw).
jrose@1474 1174 }
jrose@1145 1175 assert(src != T_VOID && dst != T_VOID, "should not be here");
jrose@1145 1176 if (src == dst) return true;
jrose@1145 1177 if (type2size[src] != type2size[dst]) return false;
jrose@2267 1178 if (src == T_OBJECT || dst == T_OBJECT) return false;
jrose@2267 1179 if (raw) return true; // bitwise reinterpretation; caller guarantees safety
jrose@1145 1180 // allow reinterpretation casts for integral widening
jrose@1145 1181 if (is_subword_type(src)) { // subwords can fit in int or other subwords
jrose@1145 1182 if (dst == T_INT) // any subword fits in an int
jrose@1145 1183 return true;
jrose@1145 1184 if (src == T_BOOLEAN) // boolean fits in any subword
jrose@1145 1185 return is_subword_type(dst);
jrose@1145 1186 if (src == T_BYTE && dst == T_SHORT)
jrose@1145 1187 return true; // remaining case: byte fits in short
jrose@1145 1188 }
jrose@1145 1189 // allow float/fixed reinterpretation casts
twisti@2903 1190 if (is_float_fixed_reinterpretation_cast(src, dst))
twisti@2903 1191 return true;
jrose@1145 1192 return false;
jrose@1145 1193 }
jrose@1145 1194
jrose@1145 1195 const char* MethodHandles::check_method_receiver(methodOop m,
jrose@1145 1196 klassOop passed_recv_type) {
jrose@1145 1197 assert(!m->is_static(), "caller resp.");
jrose@1145 1198 if (passed_recv_type == NULL)
jrose@1145 1199 return "receiver type is primitive";
jrose@1145 1200 if (class_cast_needed(passed_recv_type, m->method_holder())) {
jrose@1145 1201 Klass* formal = Klass::cast(m->method_holder());
jrose@1145 1202 return SharedRuntime::generate_class_cast_message("receiver type",
jrose@1145 1203 formal->external_name());
jrose@1145 1204 }
jrose@1145 1205 return NULL; // checks passed
jrose@1145 1206 }
jrose@1145 1207
jrose@1145 1208 // Verify that m's signature can be called type-safely by a method handle
jrose@1145 1209 // of the given method type 'mtype'.
jrose@1145 1210 // It takes a TRAPS argument because it must perform symbol lookups.
jrose@1145 1211 void MethodHandles::verify_method_signature(methodHandle m,
jrose@1145 1212 Handle mtype,
jrose@1145 1213 int first_ptype_pos,
jrose@1145 1214 KlassHandle insert_ptype,
jrose@1145 1215 TRAPS) {
jrose@2760 1216 Handle mhi_type;
jrose@2760 1217 if (m->is_method_handle_invoke()) {
jrose@2760 1218 // use this more exact typing instead of the symbolic signature:
jrose@2760 1219 mhi_type = Handle(THREAD, m->method_handle_type());
jrose@2760 1220 }
jrose@2639 1221 objArrayHandle ptypes(THREAD, java_lang_invoke_MethodType::ptypes(mtype()));
jrose@1145 1222 int pnum = first_ptype_pos;
jrose@1145 1223 int pmax = ptypes->length();
jrose@2760 1224 int anum = 0; // method argument
jrose@1145 1225 const char* err = NULL;
coleenp@2497 1226 ResourceMark rm(THREAD);
jrose@1145 1227 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
jrose@1145 1228 oop ptype_oop = NULL;
jrose@1145 1229 if (ss.at_return_type()) {
jrose@1145 1230 if (pnum != pmax)
jrose@1145 1231 { err = "too many arguments"; break; }
jrose@2639 1232 ptype_oop = java_lang_invoke_MethodType::rtype(mtype());
jrose@1145 1233 } else {
jrose@1145 1234 if (pnum >= pmax)
jrose@1145 1235 { err = "not enough arguments"; break; }
jrose@1145 1236 if (pnum >= 0)
jrose@1145 1237 ptype_oop = ptypes->obj_at(pnum);
jrose@1145 1238 else if (insert_ptype.is_null())
jrose@1145 1239 ptype_oop = NULL;
jrose@1145 1240 else
jrose@1145 1241 ptype_oop = insert_ptype->java_mirror();
jrose@1145 1242 pnum += 1;
jrose@2760 1243 anum += 1;
jrose@1145 1244 }
jrose@2760 1245 KlassHandle pklass;
jrose@2760 1246 BasicType ptype = T_OBJECT;
jrose@2760 1247 bool have_ptype = false;
jrose@2760 1248 // missing ptype_oop does not match any non-reference; use Object to report the error
jrose@2760 1249 pklass = SystemDictionaryHandles::Object_klass();
jrose@2760 1250 if (ptype_oop != NULL) {
jrose@2760 1251 have_ptype = true;
jrose@2760 1252 klassOop pklass_oop = NULL;
jrose@2760 1253 ptype = java_lang_Class::as_BasicType(ptype_oop, &pklass_oop);
jrose@2760 1254 pklass = KlassHandle(THREAD, pklass_oop);
jrose@2760 1255 }
jrose@2760 1256 ptype_oop = NULL; //done with this
jrose@2760 1257 KlassHandle aklass;
jrose@2760 1258 BasicType atype = ss.type();
jrose@2760 1259 if (atype == T_ARRAY) atype = T_OBJECT; // fold all refs to T_OBJECT
jrose@2760 1260 if (atype == T_OBJECT) {
jrose@2760 1261 if (!have_ptype) {
jrose@1145 1262 // null matches any reference
jrose@1145 1263 continue;
jrose@1145 1264 }
jrose@2760 1265 if (mhi_type.is_null()) {
jrose@2760 1266 // If we fail to resolve types at this point, we will usually throw an error.
jrose@2760 1267 TempNewSymbol name = ss.as_symbol_or_null();
jrose@2760 1268 if (name != NULL) {
jrose@2760 1269 instanceKlass* mk = instanceKlass::cast(m->method_holder());
jrose@2760 1270 Handle loader(THREAD, mk->class_loader());
jrose@2760 1271 Handle domain(THREAD, mk->protection_domain());
jrose@2760 1272 klassOop aklass_oop = SystemDictionary::resolve_or_null(name, loader, domain, CHECK);
jrose@2760 1273 if (aklass_oop != NULL)
jrose@2760 1274 aklass = KlassHandle(THREAD, aklass_oop);
jrose@2982 1275 if (aklass.is_null() &&
jrose@2982 1276 pklass.not_null() &&
jrose@2982 1277 loader.is_null() &&
jrose@2982 1278 pklass->name() == name)
jrose@2982 1279 // accept name equivalence here, since that's the best we can do
jrose@2982 1280 aklass = pklass;
jrose@2760 1281 }
jrose@2760 1282 } else {
jrose@2760 1283 // for method handle invokers we don't look at the name in the signature
jrose@2760 1284 oop atype_oop;
jrose@2760 1285 if (ss.at_return_type())
jrose@2760 1286 atype_oop = java_lang_invoke_MethodType::rtype(mhi_type());
jrose@2760 1287 else
jrose@2760 1288 atype_oop = java_lang_invoke_MethodType::ptype(mhi_type(), anum-1);
jrose@2760 1289 klassOop aklass_oop = NULL;
jrose@2760 1290 atype = java_lang_Class::as_BasicType(atype_oop, &aklass_oop);
jrose@2760 1291 aklass = KlassHandle(THREAD, aklass_oop);
jrose@1863 1292 }
jrose@1145 1293 }
jrose@1145 1294 if (!ss.at_return_type()) {
jrose@2760 1295 err = check_argument_type_change(ptype, pklass(), atype, aklass(), anum);
jrose@1145 1296 } else {
jrose@2760 1297 err = check_return_type_change(atype, aklass(), ptype, pklass()); // note reversal!
jrose@1145 1298 }
jrose@1145 1299 if (err != NULL) break;
jrose@1145 1300 }
jrose@1145 1301
jrose@1145 1302 if (err != NULL) {
jrose@2760 1303 #ifndef PRODUCT
jrose@2760 1304 if (PrintMiscellaneous && (Verbose || WizardMode)) {
jrose@2760 1305 tty->print("*** verify_method_signature failed: ");
jrose@2760 1306 java_lang_invoke_MethodType::print_signature(mtype(), tty);
jrose@2760 1307 tty->cr();
jrose@2760 1308 tty->print_cr(" first_ptype_pos = %d, insert_ptype = "UINTX_FORMAT, first_ptype_pos, insert_ptype());
jrose@2760 1309 tty->print(" Failing method: ");
jrose@2760 1310 m->print();
jrose@2760 1311 }
jrose@2760 1312 #endif //PRODUCT
jrose@1145 1313 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
jrose@1145 1314 }
jrose@1145 1315 }
jrose@1145 1316
jrose@1145 1317 // Main routine for verifying the MethodHandle.type of a proposed
jrose@1145 1318 // direct or bound-direct method handle.
jrose@1145 1319 void MethodHandles::verify_method_type(methodHandle m,
jrose@1145 1320 Handle mtype,
jrose@1145 1321 bool has_bound_recv,
jrose@1145 1322 KlassHandle bound_recv_type,
jrose@1145 1323 TRAPS) {
jrose@1145 1324 bool m_needs_receiver = !m->is_static();
jrose@1145 1325
jrose@1145 1326 const char* err = NULL;
jrose@1145 1327
jrose@1145 1328 int first_ptype_pos = m_needs_receiver ? 1 : 0;
jrose@1474 1329 if (has_bound_recv) {
jrose@1474 1330 first_ptype_pos -= 1; // ptypes do not include the bound argument; start earlier in them
jrose@1145 1331 if (m_needs_receiver && bound_recv_type.is_null())
jrose@1145 1332 { err = "bound receiver is not an object"; goto die; }
jrose@1145 1333 }
jrose@1145 1334
jrose@1145 1335 if (m_needs_receiver && err == NULL) {
jrose@2639 1336 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(mtype());
jrose@1145 1337 if (ptypes->length() < first_ptype_pos)
jrose@1145 1338 { err = "receiver argument is missing"; goto die; }
jrose@1474 1339 if (has_bound_recv)
jrose@1145 1340 err = check_method_receiver(m(), bound_recv_type->as_klassOop());
jrose@1145 1341 else
jrose@1474 1342 err = check_method_receiver(m(), java_lang_Class::as_klassOop(ptypes->obj_at(first_ptype_pos-1)));
jrose@1145 1343 if (err != NULL) goto die;
jrose@1145 1344 }
jrose@1145 1345
jrose@1145 1346 // Check the other arguments for mistypes.
jrose@1145 1347 verify_method_signature(m, mtype, first_ptype_pos, bound_recv_type, CHECK);
jrose@1145 1348 return;
jrose@1145 1349
jrose@1145 1350 die:
jrose@1145 1351 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
jrose@1145 1352 }
jrose@1145 1353
jrose@1145 1354 void MethodHandles::verify_vmslots(Handle mh, TRAPS) {
jrose@1145 1355 // Verify vmslots.
jrose@2639 1356 int check_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(mh()));
jrose@2639 1357 if (java_lang_invoke_MethodHandle::vmslots(mh()) != check_slots) {
jrose@1145 1358 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH");
jrose@1145 1359 }
jrose@1145 1360 }
jrose@1145 1361
jrose@1145 1362 void MethodHandles::verify_vmargslot(Handle mh, int argnum, int argslot, TRAPS) {
jrose@1145 1363 // Verify that argslot points at the given argnum.
jrose@2639 1364 int check_slot = argument_slot(java_lang_invoke_MethodHandle::type(mh()), argnum);
jrose@1145 1365 if (argslot != check_slot || argslot < 0) {
never@2937 1366 ResourceMark rm;
jrose@1145 1367 const char* fmt = "for argnum of %d, vmargslot is %d, should be %d";
jrose@1145 1368 size_t msglen = strlen(fmt) + 3*11 + 1;
jrose@1145 1369 char* msg = NEW_RESOURCE_ARRAY(char, msglen);
jrose@1145 1370 jio_snprintf(msg, msglen, fmt, argnum, argslot, check_slot);
jrose@1145 1371 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
jrose@1145 1372 }
jrose@1145 1373 }
jrose@1145 1374
jrose@1145 1375 // Verify the correspondence between two method types.
jrose@1145 1376 // Apart from the advertised changes, caller method type X must
jrose@1145 1377 // be able to invoke the callee method Y type with no violations
jrose@1145 1378 // of type integrity.
jrose@1145 1379 // Return NULL if all is well, else a short error message.
jrose@1145 1380 const char* MethodHandles::check_method_type_change(oop src_mtype, int src_beg, int src_end,
jrose@1145 1381 int insert_argnum, oop insert_type,
jrose@1145 1382 int change_argnum, oop change_type,
jrose@1145 1383 int delete_argnum,
jrose@1474 1384 oop dst_mtype, int dst_beg, int dst_end,
jrose@1474 1385 bool raw) {
jrose@2639 1386 objArrayOop src_ptypes = java_lang_invoke_MethodType::ptypes(src_mtype);
jrose@2639 1387 objArrayOop dst_ptypes = java_lang_invoke_MethodType::ptypes(dst_mtype);
jrose@1145 1388
jrose@1145 1389 int src_max = src_ptypes->length();
jrose@1145 1390 int dst_max = dst_ptypes->length();
jrose@1145 1391
jrose@1145 1392 if (src_end == -1) src_end = src_max;
jrose@1145 1393 if (dst_end == -1) dst_end = dst_max;
jrose@1145 1394
jrose@1145 1395 assert(0 <= src_beg && src_beg <= src_end && src_end <= src_max, "oob");
jrose@1145 1396 assert(0 <= dst_beg && dst_beg <= dst_end && dst_end <= dst_max, "oob");
jrose@1145 1397
jrose@1145 1398 // pending actions; set to -1 when done:
jrose@1145 1399 int ins_idx = insert_argnum, chg_idx = change_argnum, del_idx = delete_argnum;
jrose@1145 1400
jrose@1145 1401 const char* err = NULL;
jrose@1145 1402
jrose@1145 1403 // Walk along each array of parameter types, including a virtual
jrose@1145 1404 // NULL end marker at the end of each.
jrose@1145 1405 for (int src_idx = src_beg, dst_idx = dst_beg;
jrose@1145 1406 (src_idx <= src_end && dst_idx <= dst_end);
jrose@1145 1407 src_idx++, dst_idx++) {
jrose@1145 1408 oop src_type = (src_idx == src_end) ? oop(NULL) : src_ptypes->obj_at(src_idx);
jrose@1145 1409 oop dst_type = (dst_idx == dst_end) ? oop(NULL) : dst_ptypes->obj_at(dst_idx);
jrose@1145 1410 bool fix_null_src_type = false;
jrose@1145 1411
jrose@1145 1412 // Perform requested edits.
jrose@1145 1413 if (ins_idx == src_idx) {
jrose@1145 1414 // note that the inserted guy is never affected by a change or deletion
jrose@1145 1415 ins_idx = -1;
jrose@1145 1416 src_type = insert_type;
jrose@1145 1417 fix_null_src_type = true;
jrose@1145 1418 --src_idx; // back up to process src type on next loop
jrose@1145 1419 src_idx = src_end;
jrose@1145 1420 } else {
jrose@1145 1421 // note that the changed guy can be immediately deleted
jrose@1145 1422 if (chg_idx == src_idx) {
jrose@1145 1423 chg_idx = -1;
jrose@1145 1424 assert(src_idx < src_end, "oob");
jrose@1145 1425 src_type = change_type;
jrose@1145 1426 fix_null_src_type = true;
jrose@1145 1427 }
jrose@1145 1428 if (del_idx == src_idx) {
jrose@1145 1429 del_idx = -1;
jrose@1145 1430 assert(src_idx < src_end, "oob");
jrose@1145 1431 --dst_idx;
jrose@1145 1432 continue; // rerun loop after skipping this position
jrose@1145 1433 }
jrose@1145 1434 }
jrose@1145 1435
jrose@1145 1436 if (src_type == NULL && fix_null_src_type)
jrose@1145 1437 // explicit null in this case matches any dest reference
jrose@1145 1438 src_type = (java_lang_Class::is_primitive(dst_type) ? object_java_mirror() : dst_type);
jrose@1145 1439
jrose@1145 1440 // Compare the two argument types.
jrose@1145 1441 if (src_type != dst_type) {
jrose@1145 1442 if (src_type == NULL) return "not enough arguments";
jrose@1145 1443 if (dst_type == NULL) return "too many arguments";
jrose@1474 1444 err = check_argument_type_change(src_type, dst_type, dst_idx, raw);
jrose@1145 1445 if (err != NULL) return err;
jrose@1145 1446 }
jrose@1145 1447 }
jrose@1145 1448
jrose@1145 1449 // Now compare return types also.
jrose@2639 1450 oop src_rtype = java_lang_invoke_MethodType::rtype(src_mtype);
jrose@2639 1451 oop dst_rtype = java_lang_invoke_MethodType::rtype(dst_mtype);
jrose@1145 1452 if (src_rtype != dst_rtype) {
jrose@1474 1453 err = check_return_type_change(dst_rtype, src_rtype, raw); // note reversal!
jrose@1145 1454 if (err != NULL) return err;
jrose@1145 1455 }
jrose@1145 1456
jrose@1145 1457 assert(err == NULL, "");
jrose@1145 1458 return NULL; // all is well
jrose@1145 1459 }
jrose@1145 1460
jrose@1145 1461
jrose@1145 1462 const char* MethodHandles::check_argument_type_change(BasicType src_type,
jrose@1474 1463 klassOop src_klass,
jrose@1474 1464 BasicType dst_type,
jrose@1474 1465 klassOop dst_klass,
jrose@1474 1466 int argnum,
jrose@1474 1467 bool raw) {
jrose@1145 1468 const char* err = NULL;
twisti@2903 1469 const bool for_return = (argnum < 0);
jrose@1145 1470
jrose@1145 1471 // just in case:
jrose@1145 1472 if (src_type == T_ARRAY) src_type = T_OBJECT;
jrose@1145 1473 if (dst_type == T_ARRAY) dst_type = T_OBJECT;
jrose@1145 1474
jrose@1145 1475 // Produce some nice messages if VerifyMethodHandles is turned on:
jrose@1474 1476 if (!same_basic_type_for_arguments(src_type, dst_type, raw, for_return)) {
jrose@1145 1477 if (src_type == T_OBJECT) {
twisti@2903 1478 if (raw && is_java_primitive(dst_type))
twisti@2903 1479 return NULL; // ref-to-prim discards ref and returns zero
twisti@2903 1480 err = (!for_return
jrose@1145 1481 ? "type mismatch: passing a %s for method argument #%d, which expects primitive %s"
jrose@1145 1482 : "type mismatch: returning a %s, but caller expects primitive %s");
jrose@1145 1483 } else if (dst_type == T_OBJECT) {
twisti@2903 1484 err = (!for_return
jrose@1145 1485 ? "type mismatch: passing a primitive %s for method argument #%d, which expects %s"
jrose@1145 1486 : "type mismatch: returning a primitive %s, but caller expects %s");
jrose@1145 1487 } else {
twisti@2903 1488 err = (!for_return
jrose@1145 1489 ? "type mismatch: passing a %s for method argument #%d, which expects %s"
jrose@1145 1490 : "type mismatch: returning a %s, but caller expects %s");
jrose@1145 1491 }
jrose@1474 1492 } else if (src_type == T_OBJECT && dst_type == T_OBJECT &&
jrose@1474 1493 class_cast_needed(src_klass, dst_klass)) {
jrose@1145 1494 if (!class_cast_needed(dst_klass, src_klass)) {
jrose@1474 1495 if (raw)
jrose@1474 1496 return NULL; // reverse cast is OK; the MH target is trusted to enforce it
twisti@2903 1497 err = (!for_return
jrose@1145 1498 ? "cast required: passing a %s for method argument #%d, which expects %s"
jrose@1145 1499 : "cast required: returning a %s, but caller expects %s");
jrose@1145 1500 } else {
twisti@2903 1501 err = (!for_return
jrose@1145 1502 ? "reference mismatch: passing a %s for method argument #%d, which expects %s"
jrose@1145 1503 : "reference mismatch: returning a %s, but caller expects %s");
jrose@1145 1504 }
jrose@1145 1505 } else {
jrose@1145 1506 // passed the obstacle course
jrose@1145 1507 return NULL;
jrose@1145 1508 }
jrose@1145 1509
jrose@1145 1510 // format, format, format
jrose@1145 1511 const char* src_name = type2name(src_type);
jrose@1145 1512 const char* dst_name = type2name(dst_type);
jrose@1145 1513 if (src_name == NULL) src_name = "unknown type";
jrose@1145 1514 if (dst_name == NULL) dst_name = "unknown type";
jrose@2760 1515 if (src_type == T_OBJECT)
jrose@2760 1516 src_name = (src_klass != NULL) ? Klass::cast(src_klass)->external_name() : "an unresolved class";
jrose@2760 1517 if (dst_type == T_OBJECT)
jrose@2760 1518 dst_name = (dst_klass != NULL) ? Klass::cast(dst_klass)->external_name() : "an unresolved class";
jrose@1145 1519
jrose@1145 1520 size_t msglen = strlen(err) + strlen(src_name) + strlen(dst_name) + (argnum < 10 ? 1 : 11);
jrose@1145 1521 char* msg = NEW_RESOURCE_ARRAY(char, msglen + 1);
twisti@2903 1522 if (!for_return) {
jrose@1145 1523 assert(strstr(err, "%d") != NULL, "");
jrose@1145 1524 jio_snprintf(msg, msglen, err, src_name, argnum, dst_name);
jrose@1145 1525 } else {
jrose@1145 1526 assert(strstr(err, "%d") == NULL, "");
jrose@1145 1527 jio_snprintf(msg, msglen, err, src_name, dst_name);
jrose@1145 1528 }
jrose@1145 1529 return msg;
jrose@1145 1530 }
jrose@1145 1531
jrose@1145 1532 // Compute the depth within the stack of the given argument, i.e.,
jrose@1145 1533 // the combined size of arguments to the right of the given argument.
jrose@1145 1534 // For the last argument (ptypes.length-1) this will be zero.
jrose@1145 1535 // For the first argument (0) this will be the size of all
jrose@1145 1536 // arguments but that one. For the special number -1, this
jrose@1145 1537 // will be the size of all arguments, including the first.
jrose@1145 1538 // If the argument is neither -1 nor a valid argument index,
jrose@1145 1539 // then return a negative number. Otherwise, the result
jrose@1145 1540 // is in the range [0..vmslots] inclusive.
jrose@1145 1541 int MethodHandles::argument_slot(oop method_type, int arg) {
jrose@2639 1542 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type);
jrose@1145 1543 int argslot = 0;
jrose@1145 1544 int len = ptypes->length();
jrose@1145 1545 if (arg < -1 || arg >= len) return -99;
jrose@1145 1546 for (int i = len-1; i > arg; i--) {
jrose@1145 1547 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i));
jrose@1145 1548 argslot += type2size[bt];
jrose@1145 1549 }
jrose@1145 1550 assert(argument_slot_to_argnum(method_type, argslot) == arg, "inverse works");
jrose@1145 1551 return argslot;
jrose@1145 1552 }
jrose@1145 1553
jrose@1145 1554 // Given a slot number, return the argument number.
jrose@1145 1555 int MethodHandles::argument_slot_to_argnum(oop method_type, int query_argslot) {
jrose@2639 1556 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(method_type);
jrose@1145 1557 int argslot = 0;
jrose@1145 1558 int len = ptypes->length();
jrose@1145 1559 for (int i = len-1; i >= 0; i--) {
jrose@1145 1560 if (query_argslot == argslot) return i;
jrose@1145 1561 BasicType bt = java_lang_Class::as_BasicType(ptypes->obj_at(i));
jrose@1145 1562 argslot += type2size[bt];
jrose@1145 1563 }
jrose@1145 1564 // return pseudo-arg deepest in stack:
jrose@1145 1565 if (query_argslot == argslot) return -1;
jrose@1145 1566 return -99; // oob slot, or splitting a double-slot arg
jrose@1145 1567 }
jrose@1145 1568
jrose@1145 1569 methodHandle MethodHandles::dispatch_decoded_method(methodHandle m,
jrose@1145 1570 KlassHandle receiver_limit,
jrose@1145 1571 int decode_flags,
jrose@1145 1572 KlassHandle receiver_klass,
jrose@1145 1573 TRAPS) {
jrose@1145 1574 assert((decode_flags & ~_DMF_DIRECT_MASK) == 0, "must be direct method reference");
jrose@1145 1575 assert((decode_flags & _dmf_has_receiver) != 0, "must have a receiver or first reference argument");
jrose@1145 1576
jrose@1145 1577 if (!m->is_static() &&
jrose@1145 1578 (receiver_klass.is_null() || !receiver_klass->is_subtype_of(m->method_holder())))
jrose@1145 1579 // given type does not match class of method, or receiver is null!
jrose@1145 1580 // caller should have checked this, but let's be extra careful...
jrose@1145 1581 return methodHandle();
jrose@1145 1582
jrose@1145 1583 if (receiver_limit.not_null() &&
jrose@1145 1584 (receiver_klass.not_null() && !receiver_klass->is_subtype_of(receiver_limit())))
jrose@1145 1585 // given type is not limited to the receiver type
jrose@1145 1586 // note that a null receiver can match any reference value, for a static method
jrose@1145 1587 return methodHandle();
jrose@1145 1588
jrose@1145 1589 if (!(decode_flags & MethodHandles::_dmf_does_dispatch)) {
jrose@1145 1590 // pre-dispatched or static method (null receiver is OK for static)
jrose@1145 1591 return m;
jrose@1145 1592
jrose@1145 1593 } else if (receiver_klass.is_null()) {
jrose@1145 1594 // null receiver value; cannot dispatch
jrose@1145 1595 return methodHandle();
jrose@1145 1596
jrose@1145 1597 } else if (!(decode_flags & MethodHandles::_dmf_from_interface)) {
jrose@1145 1598 // perform virtual dispatch
jrose@1145 1599 int vtable_index = m->vtable_index();
jrose@1145 1600 guarantee(vtable_index >= 0, "valid vtable index");
jrose@1145 1601
jrose@1145 1602 // receiver_klass might be an arrayKlassOop but all vtables start at
jrose@1145 1603 // the same place. The cast is to avoid virtual call and assertion.
jrose@1145 1604 // See also LinkResolver::runtime_resolve_virtual_method.
jrose@1145 1605 instanceKlass* inst = (instanceKlass*)Klass::cast(receiver_klass());
jrose@1145 1606 DEBUG_ONLY(inst->verify_vtable_index(vtable_index));
jrose@1145 1607 methodOop m_oop = inst->method_at_vtable(vtable_index);
jrose@1145 1608 return methodHandle(THREAD, m_oop);
jrose@1145 1609
jrose@1145 1610 } else {
jrose@1145 1611 // perform interface dispatch
jrose@1145 1612 int itable_index = klassItable::compute_itable_index(m());
jrose@1145 1613 guarantee(itable_index >= 0, "valid itable index");
jrose@1145 1614 instanceKlass* inst = instanceKlass::cast(receiver_klass());
jrose@1145 1615 methodOop m_oop = inst->method_at_itable(m->method_holder(), itable_index, THREAD);
jrose@1145 1616 return methodHandle(THREAD, m_oop);
jrose@1145 1617 }
jrose@1145 1618 }
jrose@1145 1619
jrose@1145 1620 void MethodHandles::verify_DirectMethodHandle(Handle mh, methodHandle m, TRAPS) {
jrose@1145 1621 // Verify type.
jrose@2639 1622 Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh()));
jrose@1145 1623 verify_method_type(m, mtype, false, KlassHandle(), CHECK);
jrose@1145 1624
jrose@1145 1625 // Verify vmslots.
jrose@2639 1626 if (java_lang_invoke_MethodHandle::vmslots(mh()) != m->size_of_parameters()) {
jrose@1145 1627 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in DMH");
jrose@1145 1628 }
jrose@1145 1629 }
jrose@1145 1630
jrose@1145 1631 void MethodHandles::init_DirectMethodHandle(Handle mh, methodHandle m, bool do_dispatch, TRAPS) {
jrose@1145 1632 // Check arguments.
jrose@1145 1633 if (mh.is_null() || m.is_null() ||
jrose@1145 1634 (!do_dispatch && m->is_abstract())) {
jrose@1145 1635 THROW(vmSymbols::java_lang_InternalError());
jrose@1145 1636 }
jrose@1145 1637
jrose@1145 1638 if (VerifyMethodHandles) {
jrose@1145 1639 // The privileged code which invokes this routine should not make
jrose@1145 1640 // a mistake about types, but it's better to verify.
jrose@1145 1641 verify_DirectMethodHandle(mh, m, CHECK);
jrose@1145 1642 }
jrose@1145 1643
jrose@1145 1644 // Finally, after safety checks are done, link to the target method.
jrose@1145 1645 // We will follow the same path as the latter part of
jrose@1145 1646 // InterpreterRuntime::resolve_invoke(), which first finds the method
jrose@1145 1647 // and then decides how to populate the constant pool cache entry
jrose@1145 1648 // that links the interpreter calls to the method. We need the same
jrose@1145 1649 // bits, and will use the same calling sequence code.
jrose@1145 1650
twisti@2806 1651 int vmindex = methodOopDesc::garbage_vtable_index;
twisti@2806 1652 Handle vmtarget;
jrose@1145 1653
jrose@1145 1654 instanceKlass::cast(m->method_holder())->link_class(CHECK);
jrose@1145 1655
jrose@1145 1656 MethodHandleEntry* me = NULL;
jrose@1145 1657 if (do_dispatch && Klass::cast(m->method_holder())->is_interface()) {
jrose@1145 1658 // We are simulating an invokeinterface instruction.
jrose@1145 1659 // (We might also be simulating an invokevirtual on a miranda method,
jrose@1145 1660 // but it is safe to treat it as an invokeinterface.)
jrose@1145 1661 assert(!m->can_be_statically_bound(), "no final methods on interfaces");
jrose@1145 1662 vmindex = klassItable::compute_itable_index(m());
jrose@1145 1663 assert(vmindex >= 0, "(>=0) == do_dispatch");
jrose@1145 1664 // Set up same bits as ConstantPoolCacheEntry::set_interface_call().
jrose@1145 1665 vmtarget = m->method_holder(); // the interface
jrose@1145 1666 me = MethodHandles::entry(MethodHandles::_invokeinterface_mh);
jrose@1145 1667 } else if (!do_dispatch || m->can_be_statically_bound()) {
jrose@1145 1668 // We are simulating an invokestatic or invokespecial instruction.
jrose@1145 1669 // Set up the method pointer, just like ConstantPoolCacheEntry::set_method().
twisti@2806 1670 vmtarget = m;
jrose@1145 1671 // this does not help dispatch, but it will make it possible to parse this MH:
jrose@1145 1672 vmindex = methodOopDesc::nonvirtual_vtable_index;
jrose@1145 1673 assert(vmindex < 0, "(>=0) == do_dispatch");
jrose@1145 1674 if (!m->is_static()) {
jrose@1145 1675 me = MethodHandles::entry(MethodHandles::_invokespecial_mh);
jrose@1145 1676 } else {
jrose@1145 1677 me = MethodHandles::entry(MethodHandles::_invokestatic_mh);
jrose@1145 1678 // Part of the semantics of a static call is an initialization barrier.
jrose@1145 1679 // For a DMH, it is done now, when the handle is created.
jrose@1145 1680 Klass* k = Klass::cast(m->method_holder());
jrose@1145 1681 if (k->should_be_initialized()) {
twisti@2806 1682 k->initialize(CHECK); // possible safepoint
jrose@1145 1683 }
jrose@1145 1684 }
jrose@1145 1685 } else {
jrose@1145 1686 // We are simulating an invokevirtual instruction.
jrose@1145 1687 // Set up the vtable index, just like ConstantPoolCacheEntry::set_method().
jrose@1145 1688 // The key logic is LinkResolver::runtime_resolve_virtual_method.
jrose@1145 1689 vmindex = m->vtable_index();
jrose@1145 1690 vmtarget = m->method_holder();
jrose@1145 1691 me = MethodHandles::entry(MethodHandles::_invokevirtual_mh);
jrose@1145 1692 }
jrose@1145 1693
jrose@1145 1694 if (me == NULL) { THROW(vmSymbols::java_lang_InternalError()); }
jrose@1145 1695
twisti@2806 1696 java_lang_invoke_DirectMethodHandle::set_vmtarget(mh(), vmtarget());
twisti@2806 1697 java_lang_invoke_DirectMethodHandle::set_vmindex( mh(), vmindex);
twisti@2806 1698 DEBUG_ONLY(KlassHandle rlimit; int flags);
twisti@2806 1699 assert(MethodHandles::decode_method(mh(), rlimit, flags) == m,
jrose@1145 1700 "properly stored for later decoding");
jrose@1145 1701 DEBUG_ONLY(bool actual_do_dispatch = ((flags & _dmf_does_dispatch) != 0));
jrose@1145 1702 assert(!(actual_do_dispatch && !do_dispatch),
jrose@1145 1703 "do not perform dispatch if !do_dispatch specified");
jrose@1145 1704 assert(actual_do_dispatch == (vmindex >= 0), "proper later decoding of do_dispatch");
jrose@1145 1705 assert(decode_MethodHandle_stack_pushes(mh()) == 0, "DMH does not move stack");
jrose@1145 1706
jrose@1145 1707 // Done!
jrose@2639 1708 java_lang_invoke_MethodHandle::set_vmentry(mh(), me);
jrose@1145 1709 }
jrose@1145 1710
jrose@1145 1711 void MethodHandles::verify_BoundMethodHandle_with_receiver(Handle mh,
jrose@1145 1712 methodHandle m,
jrose@1145 1713 TRAPS) {
jrose@1145 1714 // Verify type.
twisti@2806 1715 KlassHandle bound_recv_type;
twisti@2806 1716 {
twisti@2806 1717 oop receiver = java_lang_invoke_BoundMethodHandle::argument(mh());
twisti@2806 1718 if (receiver != NULL)
twisti@2806 1719 bound_recv_type = KlassHandle(THREAD, receiver->klass());
twisti@2806 1720 }
jrose@2639 1721 Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(mh()));
jrose@1145 1722 verify_method_type(m, mtype, true, bound_recv_type, CHECK);
jrose@1145 1723
jrose@1145 1724 int receiver_pos = m->size_of_parameters() - 1;
jrose@1145 1725
jrose@1145 1726 // Verify MH.vmargslot, which should point at the bound receiver.
jrose@2639 1727 verify_vmargslot(mh, -1, java_lang_invoke_BoundMethodHandle::vmargslot(mh()), CHECK);
jrose@1145 1728 //verify_vmslots(mh, CHECK);
jrose@1145 1729
jrose@1145 1730 // Verify vmslots.
jrose@2639 1731 if (java_lang_invoke_MethodHandle::vmslots(mh()) != receiver_pos) {
jrose@1145 1732 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad vmslots in BMH (receiver)");
jrose@1145 1733 }
jrose@1145 1734 }
jrose@1145 1735
jrose@1145 1736 // Initialize a BMH with a receiver bound directly to a methodOop.
jrose@1145 1737 void MethodHandles::init_BoundMethodHandle_with_receiver(Handle mh,
jrose@1145 1738 methodHandle original_m,
jrose@1145 1739 KlassHandle receiver_limit,
jrose@1145 1740 int decode_flags,
jrose@1145 1741 TRAPS) {
jrose@1145 1742 // Check arguments.
jrose@1145 1743 if (mh.is_null() || original_m.is_null()) {
jrose@1145 1744 THROW(vmSymbols::java_lang_InternalError());
jrose@1145 1745 }
jrose@1145 1746
jrose@1145 1747 KlassHandle receiver_klass;
jrose@1145 1748 {
jrose@2639 1749 oop receiver_oop = java_lang_invoke_BoundMethodHandle::argument(mh());
jrose@1145 1750 if (receiver_oop != NULL)
jrose@1145 1751 receiver_klass = KlassHandle(THREAD, receiver_oop->klass());
jrose@1145 1752 }
jrose@1145 1753 methodHandle m = dispatch_decoded_method(original_m,
jrose@1145 1754 receiver_limit, decode_flags,
jrose@1145 1755 receiver_klass,
jrose@1145 1756 CHECK);
jrose@1145 1757 if (m.is_null()) { THROW(vmSymbols::java_lang_InternalError()); }
jrose@1145 1758 if (m->is_abstract()) { THROW(vmSymbols::java_lang_AbstractMethodError()); }
jrose@1145 1759
never@2895 1760 int vmargslot = m->size_of_parameters() - 1;
never@2895 1761 assert(java_lang_invoke_BoundMethodHandle::vmargslot(mh()) == vmargslot, "");
jrose@1145 1762
jrose@1145 1763 if (VerifyMethodHandles) {
jrose@1145 1764 verify_BoundMethodHandle_with_receiver(mh, m, CHECK);
jrose@1145 1765 }
jrose@1145 1766
jrose@2639 1767 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m());
jrose@1145 1768
twisti@2806 1769 DEBUG_ONLY(KlassHandle junk1; int junk2);
twisti@2806 1770 assert(MethodHandles::decode_method(mh(), junk1, junk2) == m, "properly stored for later decoding");
jrose@1145 1771 assert(decode_MethodHandle_stack_pushes(mh()) == 1, "BMH pushes one stack slot");
jrose@1145 1772
jrose@1145 1773 // Done!
jrose@2639 1774 java_lang_invoke_MethodHandle::set_vmentry(mh(), MethodHandles::entry(MethodHandles::_bound_ref_direct_mh));
jrose@1145 1775 }
jrose@1145 1776
jrose@1145 1777 void MethodHandles::verify_BoundMethodHandle(Handle mh, Handle target, int argnum,
jrose@1145 1778 bool direct_to_method, TRAPS) {
never@2950 1779 ResourceMark rm;
jrose@1145 1780 Handle ptype_handle(THREAD,
jrose@2639 1781 java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum));
jrose@1145 1782 KlassHandle ptype_klass;
jrose@1145 1783 BasicType ptype = java_lang_Class::as_BasicType(ptype_handle(), &ptype_klass);
jrose@1145 1784 int slots_pushed = type2size[ptype];
jrose@1145 1785
jrose@2639 1786 oop argument = java_lang_invoke_BoundMethodHandle::argument(mh());
jrose@1145 1787
jrose@1145 1788 const char* err = NULL;
jrose@1145 1789
jrose@1145 1790 switch (ptype) {
jrose@1145 1791 case T_OBJECT:
jrose@1145 1792 if (argument != NULL)
jrose@1145 1793 // we must implicitly convert from the arg type to the outgoing ptype
jrose@1145 1794 err = check_argument_type_change(T_OBJECT, argument->klass(), ptype, ptype_klass(), argnum);
jrose@1145 1795 break;
jrose@1145 1796
jrose@1145 1797 case T_ARRAY: case T_VOID:
jrose@1145 1798 assert(false, "array, void do not appear here");
jrose@1145 1799 default:
jrose@1145 1800 if (ptype != T_INT && !is_subword_type(ptype)) {
jrose@1145 1801 err = "unexpected parameter type";
jrose@1145 1802 break;
jrose@1145 1803 }
jrose@1145 1804 // check subrange of Integer.value, if necessary
never@1577 1805 if (argument == NULL || argument->klass() != SystemDictionary::Integer_klass()) {
jrose@1145 1806 err = "bound integer argument must be of type java.lang.Integer";
jrose@1145 1807 break;
jrose@1145 1808 }
jrose@1145 1809 if (ptype != T_INT) {
jrose@1145 1810 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
jrose@1145 1811 jint value = argument->int_field(value_offset);
twisti@2204 1812 int vminfo = adapter_unbox_subword_vminfo(ptype);
jrose@1145 1813 jint subword = truncate_subword_from_vminfo(value, vminfo);
jrose@1145 1814 if (value != subword) {
jrose@1145 1815 err = "bound subword value does not fit into the subword type";
jrose@1145 1816 break;
jrose@1145 1817 }
jrose@1145 1818 }
jrose@1145 1819 break;
jrose@1145 1820 case T_FLOAT:
jrose@1145 1821 case T_DOUBLE:
jrose@1145 1822 case T_LONG:
jrose@1145 1823 {
jrose@1145 1824 // we must implicitly convert from the unboxed arg type to the outgoing ptype
jrose@1145 1825 BasicType argbox = java_lang_boxing_object::basic_type(argument);
jrose@1145 1826 if (argbox != ptype) {
jrose@1145 1827 err = check_argument_type_change(T_OBJECT, (argument == NULL
never@1577 1828 ? SystemDictionary::Object_klass()
jrose@1145 1829 : argument->klass()),
jrose@1145 1830 ptype, ptype_klass(), argnum);
jrose@1145 1831 assert(err != NULL, "this must be an error");
jrose@1145 1832 }
jrose@1145 1833 break;
jrose@1145 1834 }
jrose@1145 1835 }
jrose@1145 1836
jrose@1145 1837 if (err == NULL) {
jrose@1145 1838 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh()));
jrose@1145 1839 if (direct_to_method) {
jrose@1145 1840 assert(this_pushes == slots_pushed, "BMH pushes one or two stack slots");
jrose@1145 1841 } else {
jrose@1474 1842 int target_pushes = decode_MethodHandle_stack_pushes(target());
jrose@1474 1843 assert(this_pushes == slots_pushed + target_pushes, "BMH stack motion must be correct");
jrose@1145 1844 }
jrose@1145 1845 }
jrose@1145 1846
jrose@1145 1847 if (err == NULL) {
jrose@1145 1848 // Verify the rest of the method type.
jrose@2639 1849 err = check_method_type_insertion(java_lang_invoke_MethodHandle::type(mh()),
jrose@1145 1850 argnum, ptype_handle(),
jrose@2639 1851 java_lang_invoke_MethodHandle::type(target()));
jrose@1145 1852 }
jrose@1145 1853
jrose@1145 1854 if (err != NULL) {
jrose@1145 1855 THROW_MSG(vmSymbols::java_lang_InternalError(), err);
jrose@1145 1856 }
jrose@1145 1857 }
jrose@1145 1858
jrose@1145 1859 void MethodHandles::init_BoundMethodHandle(Handle mh, Handle target, int argnum, TRAPS) {
jrose@1145 1860 // Check arguments.
jrose@2639 1861 if (mh.is_null() || target.is_null() || !java_lang_invoke_MethodHandle::is_instance(target())) {
jrose@1145 1862 THROW(vmSymbols::java_lang_InternalError());
jrose@1145 1863 }
jrose@1145 1864
never@2895 1865 int argslot = java_lang_invoke_BoundMethodHandle::vmargslot(mh());
jrose@1145 1866
jrose@1145 1867 if (VerifyMethodHandles) {
jrose@1145 1868 int insert_after = argnum - 1;
never@2895 1869 verify_vmargslot(mh, insert_after, argslot, CHECK);
jrose@1145 1870 verify_vmslots(mh, CHECK);
jrose@1145 1871 }
jrose@1145 1872
twisti@1573 1873 // Get bound type and required slots.
twisti@2806 1874 BasicType ptype;
twisti@2806 1875 {
twisti@2806 1876 oop ptype_oop = java_lang_invoke_MethodType::ptype(java_lang_invoke_MethodHandle::type(target()), argnum);
twisti@2806 1877 ptype = java_lang_Class::as_BasicType(ptype_oop);
twisti@2806 1878 }
twisti@1573 1879 int slots_pushed = type2size[ptype];
twisti@1573 1880
jrose@1145 1881 // If (a) the target is a direct non-dispatched method handle,
jrose@1145 1882 // or (b) the target is a dispatched direct method handle and we
jrose@1145 1883 // are binding the receiver, cut out the middle-man.
jrose@1145 1884 // Do this by decoding the DMH and using its methodOop directly as vmtarget.
jrose@1145 1885 bool direct_to_method = false;
jrose@1145 1886 if (OptimizeMethodHandles &&
jrose@1145 1887 target->klass() == SystemDictionary::DirectMethodHandle_klass() &&
never@2937 1888 (argnum != 0 || java_lang_invoke_BoundMethodHandle::argument(mh()) != NULL) &&
jrose@2639 1889 (argnum == 0 || java_lang_invoke_DirectMethodHandle::vmindex(target()) < 0)) {
twisti@2806 1890 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 1891 methodHandle m = decode_method(target(), receiver_limit, decode_flags);
jrose@1145 1892 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "DMH failed to decode"); }
twisti@1573 1893 DEBUG_ONLY(int m_vmslots = m->size_of_parameters() - slots_pushed); // pos. of 1st arg.
jrose@2639 1894 assert(java_lang_invoke_BoundMethodHandle::vmslots(mh()) == m_vmslots, "type w/ m sig");
jrose@1145 1895 if (argnum == 0 && (decode_flags & _dmf_has_receiver) != 0) {
jrose@1145 1896 init_BoundMethodHandle_with_receiver(mh, m,
jrose@1145 1897 receiver_limit, decode_flags,
jrose@1145 1898 CHECK);
jrose@1145 1899 return;
jrose@1145 1900 }
jrose@1145 1901
jrose@1145 1902 // Even if it is not a bound receiver, we still might be able
jrose@1145 1903 // to bind another argument and still invoke the methodOop directly.
jrose@1145 1904 if (!(decode_flags & _dmf_does_dispatch)) {
jrose@1145 1905 direct_to_method = true;
jrose@2639 1906 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), m());
jrose@1145 1907 }
jrose@1145 1908 }
jrose@1145 1909 if (!direct_to_method)
jrose@2639 1910 java_lang_invoke_BoundMethodHandle::set_vmtarget(mh(), target());
jrose@1145 1911
jrose@1145 1912 if (VerifyMethodHandles) {
jrose@1145 1913 verify_BoundMethodHandle(mh, target, argnum, direct_to_method, CHECK);
jrose@1145 1914 }
jrose@1145 1915
jrose@1145 1916 // Next question: Is this a ref, int, or long bound value?
jrose@1145 1917 MethodHandleEntry* me = NULL;
jrose@1145 1918 if (ptype == T_OBJECT) {
jrose@1145 1919 if (direct_to_method) me = MethodHandles::entry(_bound_ref_direct_mh);
jrose@1145 1920 else me = MethodHandles::entry(_bound_ref_mh);
jrose@1145 1921 } else if (slots_pushed == 2) {
jrose@1145 1922 if (direct_to_method) me = MethodHandles::entry(_bound_long_direct_mh);
jrose@1145 1923 else me = MethodHandles::entry(_bound_long_mh);
jrose@1145 1924 } else if (slots_pushed == 1) {
jrose@1145 1925 if (direct_to_method) me = MethodHandles::entry(_bound_int_direct_mh);
jrose@1145 1926 else me = MethodHandles::entry(_bound_int_mh);
jrose@1145 1927 } else {
jrose@1145 1928 assert(false, "");
jrose@1145 1929 }
jrose@1145 1930
jrose@1145 1931 // Done!
jrose@2639 1932 java_lang_invoke_MethodHandle::set_vmentry(mh(), me);
jrose@1145 1933 }
jrose@1145 1934
jrose@1145 1935 static void throw_InternalError_for_bad_conversion(int conversion, const char* err, TRAPS) {
jrose@1145 1936 char msg[200];
jrose@1145 1937 jio_snprintf(msg, sizeof(msg), "bad adapter (conversion=0x%08x): %s", conversion, err);
jrose@1145 1938 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), msg);
jrose@1145 1939 }
jrose@1145 1940
jrose@1145 1941 void MethodHandles::verify_AdapterMethodHandle(Handle mh, int argnum, TRAPS) {
never@2950 1942 ResourceMark rm;
jrose@2639 1943 jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh());
jrose@2639 1944 int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh());
jrose@1145 1945
jrose@1145 1946 verify_vmargslot(mh, argnum, argslot, CHECK);
jrose@1145 1947 verify_vmslots(mh, CHECK);
jrose@1145 1948
jrose@1145 1949 jint conv_op = adapter_conversion_op(conversion);
jrose@1145 1950 if (!conv_op_valid(conv_op)) {
jrose@1145 1951 throw_InternalError_for_bad_conversion(conversion, "unknown conversion op", THREAD);
jrose@1145 1952 return;
jrose@1145 1953 }
jrose@1145 1954 EntryKind ek = adapter_entry_kind(conv_op);
jrose@1145 1955
jrose@1145 1956 int stack_move = adapter_conversion_stack_move(conversion);
jrose@1145 1957 BasicType src = adapter_conversion_src_type(conversion);
jrose@1145 1958 BasicType dest = adapter_conversion_dest_type(conversion);
jrose@1145 1959 int vminfo = adapter_conversion_vminfo(conversion); // should be zero
jrose@1145 1960
jrose@2639 1961 Handle argument(THREAD, java_lang_invoke_AdapterMethodHandle::argument(mh()));
jrose@2639 1962 Handle target(THREAD, java_lang_invoke_AdapterMethodHandle::vmtarget(mh()));
jrose@2639 1963 Handle src_mtype(THREAD, java_lang_invoke_MethodHandle::type(mh()));
jrose@2639 1964 Handle dst_mtype(THREAD, java_lang_invoke_MethodHandle::type(target()));
never@2895 1965 Handle arg_mtype;
jrose@1145 1966
jrose@1145 1967 const char* err = NULL;
jrose@1145 1968
jrose@1145 1969 if (err == NULL) {
jrose@1145 1970 // Check that the correct argument is supplied, but only if it is required.
jrose@1145 1971 switch (ek) {
jrose@1145 1972 case _adapter_check_cast: // target type of cast
jrose@1145 1973 case _adapter_ref_to_prim: // wrapper type from which to unbox
jrose@1145 1974 case _adapter_spread_args: // array type to spread from
jrose@1145 1975 if (!java_lang_Class::is_instance(argument())
jrose@1145 1976 || java_lang_Class::is_primitive(argument()))
jrose@1145 1977 { err = "adapter requires argument of type java.lang.Class"; break; }
never@2895 1978 if (ek == _adapter_spread_args) {
jrose@1145 1979 // Make sure it is a suitable collection type. (Array, for now.)
jrose@1145 1980 Klass* ak = Klass::cast(java_lang_Class::as_klassOop(argument()));
never@2895 1981 if (!ak->oop_is_array())
never@2895 1982 { err = "spread adapter requires argument representing an array class"; break; }
never@2895 1983 BasicType et = arrayKlass::cast(ak->as_klassOop())->element_type();
never@2895 1984 if (et != dest && stack_move <= 0)
never@2895 1985 { err = "spread adapter requires array class argument of correct type"; break; }
jrose@1145 1986 }
jrose@1145 1987 break;
never@2895 1988 case _adapter_prim_to_ref: // boxer MH to use
never@2895 1989 case _adapter_collect_args: // method handle which collects the args
never@2895 1990 case _adapter_fold_args: // method handle which collects the args
never@2895 1991 if (!UseRicochetFrames) {
never@2895 1992 { err = "box/collect/fold operators are not supported"; break; }
never@2895 1993 }
jrose@2639 1994 if (!java_lang_invoke_MethodHandle::is_instance(argument()))
jrose@1145 1995 { err = "MethodHandle adapter argument required"; break; }
never@2895 1996 arg_mtype = Handle(THREAD, java_lang_invoke_MethodHandle::type(argument()));
jrose@1145 1997 break;
jrose@1145 1998 default:
jrose@1145 1999 if (argument.not_null())
jrose@1145 2000 { err = "adapter has spurious argument"; break; }
jrose@1145 2001 break;
jrose@1145 2002 }
jrose@1145 2003 }
jrose@1145 2004
jrose@1145 2005 if (err == NULL) {
jrose@1145 2006 // Check that the src/dest types are supplied if needed.
never@2895 2007 // Also check relevant parameter or return types.
jrose@1145 2008 switch (ek) {
jrose@1474 2009 case _adapter_check_cast:
jrose@1474 2010 if (src != T_OBJECT || dest != T_OBJECT) {
jrose@1474 2011 err = "adapter requires object src/dest conversion subfields";
jrose@1474 2012 }
jrose@1474 2013 break;
jrose@1145 2014 case _adapter_prim_to_prim:
jrose@1145 2015 if (!is_java_primitive(src) || !is_java_primitive(dest) || src == dest) {
jrose@1145 2016 err = "adapter requires primitive src/dest conversion subfields"; break;
jrose@1145 2017 }
jrose@1145 2018 if ( (src == T_FLOAT || src == T_DOUBLE) && !(dest == T_FLOAT || dest == T_DOUBLE) ||
jrose@1145 2019 !(src == T_FLOAT || src == T_DOUBLE) && (dest == T_FLOAT || dest == T_DOUBLE)) {
jrose@1145 2020 err = "adapter cannot convert beween floating and fixed-point"; break;
jrose@1145 2021 }
jrose@1145 2022 break;
jrose@1145 2023 case _adapter_ref_to_prim:
jrose@1145 2024 if (src != T_OBJECT || !is_java_primitive(dest)
jrose@1145 2025 || argument() != Klass::cast(SystemDictionary::box_klass(dest))->java_mirror()) {
jrose@1145 2026 err = "adapter requires primitive dest conversion subfield"; break;
jrose@1145 2027 }
jrose@1145 2028 break;
jrose@1145 2029 case _adapter_prim_to_ref:
never@2895 2030 if (!is_java_primitive(src) || dest != T_OBJECT) {
jrose@1145 2031 err = "adapter requires primitive src conversion subfield"; break;
jrose@1145 2032 }
jrose@1145 2033 break;
jrose@1145 2034 case _adapter_swap_args:
jrose@1145 2035 {
never@2954 2036 if (!src || !dest) {
jrose@1145 2037 err = "adapter requires src/dest conversion subfields for swap"; break;
jrose@1145 2038 }
never@2954 2039 int src_size = type2size[src];
never@2954 2040 if (src_size != type2size[dest]) {
never@2954 2041 err = "adapter requires equal sizes for src/dest"; break;
never@2954 2042 }
jrose@1145 2043 int src_slot = argslot;
jrose@1145 2044 int dest_slot = vminfo;
jrose@1145 2045 int src_arg = argnum;
never@2954 2046 int dest_arg = argument_slot_to_argnum(src_mtype(), dest_slot);
never@2954 2047 verify_vmargslot(mh, dest_arg, dest_slot, CHECK);
never@2954 2048 if (!(dest_slot >= src_slot + src_size) &&
never@2954 2049 !(src_slot >= dest_slot + src_size)) {
never@2954 2050 err = "source, destination slots must be distinct"; break;
never@2954 2051 } else if (!(src_slot > dest_slot)) {
never@2954 2052 err = "source of swap must be deeper in stack"; break;
jrose@1145 2053 }
never@2954 2054 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), dest_arg),
never@2954 2055 java_lang_invoke_MethodType::ptype(dst_mtype(), src_arg),
never@2954 2056 dest_arg);
jrose@1145 2057 if (err == NULL)
never@2895 2058 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), src_arg),
never@2895 2059 java_lang_invoke_MethodType::ptype(dst_mtype(), dest_arg),
jrose@1145 2060 src_arg);
never@2954 2061 break;
never@2954 2062 }
never@2954 2063 case _adapter_rot_args:
never@2954 2064 {
never@2954 2065 if (!src || !dest) {
never@2954 2066 err = "adapter requires src/dest conversion subfields for rotate"; break;
never@2954 2067 }
never@2954 2068 int src_slot = argslot;
never@2954 2069 int limit_raw = vminfo;
never@2954 2070 bool rot_down = (src_slot < limit_raw);
never@2954 2071 int limit_bias = (rot_down ? MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS : 0);
never@2954 2072 int limit_slot = limit_raw - limit_bias;
never@2954 2073 int src_arg = argnum;
never@2954 2074 int limit_arg = argument_slot_to_argnum(src_mtype(), limit_slot);
never@2954 2075 verify_vmargslot(mh, limit_arg, limit_slot, CHECK);
never@2954 2076 if (src_slot == limit_slot) {
never@2954 2077 err = "source, destination slots must be distinct"; break;
never@2954 2078 }
never@2954 2079 if (!rot_down) { // rotate slots up == shift arguments left
never@2954 2080 // limit_slot is an inclusive lower limit
never@2954 2081 assert((src_slot > limit_slot) && (src_arg < limit_arg), "");
never@2954 2082 // rotate up: [limit_slot..src_slot-ss] --> [limit_slot+ss..src_slot]
never@2954 2083 // that is: [src_arg+1..limit_arg] --> [src_arg..limit_arg-1]
never@2954 2084 for (int i = src_arg+1; i <= limit_arg && err == NULL; i++) {
never@2954 2085 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), i),
never@2954 2086 java_lang_invoke_MethodType::ptype(dst_mtype(), i-1),
never@2954 2087 i);
never@2954 2088 }
never@2954 2089 } else { // rotate slots down == shfit arguments right
never@2954 2090 // limit_slot is an exclusive upper limit
never@2954 2091 assert((src_slot < limit_slot - limit_bias) && (src_arg > limit_arg + limit_bias), "");
never@2954 2092 // rotate down: [src_slot+ss..limit_slot) --> [src_slot..limit_slot-ss)
never@2954 2093 // that is: (limit_arg..src_arg-1] --> (dst_arg+1..src_arg]
never@2954 2094 for (int i = limit_arg+1; i <= src_arg-1 && err == NULL; i++) {
never@2954 2095 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), i),
never@2954 2096 java_lang_invoke_MethodType::ptype(dst_mtype(), i+1),
never@2954 2097 i);
never@2954 2098 }
never@2954 2099 }
never@2954 2100 if (err == NULL) {
never@2954 2101 int dest_arg = (rot_down ? limit_arg+1 : limit_arg);
never@2954 2102 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), src_arg),
never@2954 2103 java_lang_invoke_MethodType::ptype(dst_mtype(), dest_arg),
never@2954 2104 src_arg);
never@2954 2105 }
jrose@1145 2106 }
jrose@1145 2107 break;
never@2895 2108 case _adapter_spread_args:
jrose@1145 2109 case _adapter_collect_args:
never@2895 2110 case _adapter_fold_args:
jrose@1145 2111 {
never@2895 2112 bool is_spread = (ek == _adapter_spread_args);
never@2895 2113 bool is_fold = (ek == _adapter_fold_args);
never@2895 2114 BasicType coll_type = is_spread ? src : dest;
never@2895 2115 BasicType elem_type = is_spread ? dest : src;
never@2895 2116 // coll_type is type of args in collected form (or T_VOID if none)
never@2895 2117 // elem_type is common type of args in spread form (or T_VOID if missing or heterogeneous)
never@2895 2118 if (coll_type == 0 || elem_type == 0) {
never@2895 2119 err = "adapter requires src/dest subfields for spread or collect"; break;
never@2895 2120 }
never@2895 2121 if (is_spread && coll_type != T_OBJECT) {
never@2895 2122 err = "spread adapter requires object type for argument bundle"; break;
never@2895 2123 }
never@2895 2124 Handle spread_mtype = (is_spread ? dst_mtype : src_mtype);
never@2895 2125 int spread_slot = argslot;
never@2895 2126 int spread_arg = argnum;
never@2895 2127 int slots_pushed = stack_move / stack_move_unit();
never@2895 2128 int coll_slot_count = type2size[coll_type];
never@2895 2129 int spread_slot_count = (is_spread ? slots_pushed : -slots_pushed) + coll_slot_count;
never@2895 2130 if (is_fold) spread_slot_count = argument_slot_count(arg_mtype());
never@2895 2131 if (!is_spread) {
never@2895 2132 int init_slots = argument_slot_count(src_mtype());
never@2895 2133 int coll_slots = argument_slot_count(arg_mtype());
never@2895 2134 if (spread_slot_count > init_slots ||
never@2895 2135 spread_slot_count != coll_slots) {
never@2895 2136 err = "collect adapter has inconsistent arg counts"; break;
never@2895 2137 }
never@2895 2138 int next_slots = argument_slot_count(dst_mtype());
never@2895 2139 int unchanged_slots_in = (init_slots - spread_slot_count);
never@2895 2140 int unchanged_slots_out = (next_slots - coll_slot_count - (is_fold ? spread_slot_count : 0));
never@2895 2141 if (unchanged_slots_in != unchanged_slots_out) {
never@2895 2142 err = "collect adapter continuation has inconsistent arg counts"; break;
never@2895 2143 }
jrose@1145 2144 }
jrose@1145 2145 }
jrose@1145 2146 break;
jrose@1145 2147 default:
jrose@1145 2148 if (src != 0 || dest != 0) {
jrose@1145 2149 err = "adapter has spurious src/dest conversion subfields"; break;
jrose@1145 2150 }
jrose@1145 2151 break;
jrose@1145 2152 }
jrose@1145 2153 }
jrose@1145 2154
jrose@1145 2155 if (err == NULL) {
jrose@1145 2156 // Check the stack_move subfield.
jrose@1145 2157 // It must always report the net change in stack size, positive or negative.
jrose@1145 2158 int slots_pushed = stack_move / stack_move_unit();
jrose@1145 2159 switch (ek) {
jrose@1145 2160 case _adapter_prim_to_prim:
jrose@1145 2161 case _adapter_ref_to_prim:
jrose@1145 2162 case _adapter_prim_to_ref:
jrose@1145 2163 if (slots_pushed != type2size[dest] - type2size[src]) {
jrose@1145 2164 err = "wrong stack motion for primitive conversion";
jrose@1145 2165 }
jrose@1145 2166 break;
jrose@1145 2167 case _adapter_dup_args:
jrose@1145 2168 if (slots_pushed <= 0) {
jrose@1145 2169 err = "adapter requires conversion subfield slots_pushed > 0";
jrose@1145 2170 }
jrose@1145 2171 break;
jrose@1145 2172 case _adapter_drop_args:
jrose@1145 2173 if (slots_pushed >= 0) {
jrose@1145 2174 err = "adapter requires conversion subfield slots_pushed < 0";
jrose@1145 2175 }
jrose@1145 2176 break;
jrose@1145 2177 case _adapter_collect_args:
never@2895 2178 case _adapter_fold_args:
never@2895 2179 if (slots_pushed > 2) {
never@2895 2180 err = "adapter requires conversion subfield slots_pushed <= 2";
jrose@1145 2181 }
jrose@1145 2182 break;
jrose@1145 2183 case _adapter_spread_args:
jrose@1145 2184 if (slots_pushed < -1) {
jrose@1145 2185 err = "adapter requires conversion subfield slots_pushed >= -1";
jrose@1145 2186 }
jrose@1145 2187 break;
jrose@1145 2188 default:
jrose@1145 2189 if (stack_move != 0) {
jrose@1145 2190 err = "adapter has spurious stack_move conversion subfield";
jrose@1145 2191 }
jrose@1145 2192 break;
jrose@1145 2193 }
jrose@1145 2194 if (err == NULL && stack_move != slots_pushed * stack_move_unit()) {
jrose@1145 2195 err = "stack_move conversion subfield must be multiple of stack_move_unit";
jrose@1145 2196 }
jrose@1145 2197 }
jrose@1145 2198
jrose@1145 2199 if (err == NULL) {
never@2895 2200 // Make sure this adapter's stack pushing is accurately recorded.
jrose@1145 2201 int slots_pushed = stack_move / stack_move_unit();
jrose@2639 2202 int this_vmslots = java_lang_invoke_MethodHandle::vmslots(mh());
jrose@2639 2203 int target_vmslots = java_lang_invoke_MethodHandle::vmslots(target());
never@2895 2204 int target_pushes = decode_MethodHandle_stack_pushes(target());
jrose@1474 2205 if (slots_pushed != (target_vmslots - this_vmslots)) {
jrose@1145 2206 err = "stack_move inconsistent with previous and current MethodType vmslots";
never@2895 2207 } else {
never@2895 2208 int this_pushes = decode_MethodHandle_stack_pushes(mh());
never@2895 2209 if (slots_pushed + target_pushes != this_pushes) {
never@2895 2210 if (this_pushes == 0)
never@2895 2211 err = "adapter push count not initialized";
never@2895 2212 else
never@2895 2213 err = "adapter push count is wrong";
jrose@1145 2214 }
jrose@1145 2215 }
jrose@1145 2216
jrose@1145 2217 // While we're at it, check that the stack motion decoder works:
jrose@1145 2218 DEBUG_ONLY(int this_pushes = decode_MethodHandle_stack_pushes(mh()));
jrose@1474 2219 assert(this_pushes == slots_pushed + target_pushes, "AMH stack motion must be correct");
jrose@1145 2220 }
jrose@1145 2221
jrose@1145 2222 if (err == NULL && vminfo != 0) {
jrose@1145 2223 switch (ek) {
never@2895 2224 case _adapter_swap_args:
never@2895 2225 case _adapter_rot_args:
never@2895 2226 case _adapter_prim_to_ref:
never@2895 2227 case _adapter_collect_args:
never@2895 2228 case _adapter_fold_args:
never@2895 2229 break; // OK
jrose@1145 2230 default:
jrose@1145 2231 err = "vminfo subfield is reserved to the JVM";
jrose@1145 2232 }
jrose@1145 2233 }
jrose@1145 2234
jrose@1145 2235 // Do additional ad hoc checks.
jrose@1145 2236 if (err == NULL) {
jrose@1145 2237 switch (ek) {
jrose@1145 2238 case _adapter_retype_only:
jrose@1474 2239 err = check_method_type_passthrough(src_mtype(), dst_mtype(), false);
jrose@1474 2240 break;
jrose@1474 2241
jrose@1474 2242 case _adapter_retype_raw:
jrose@1474 2243 err = check_method_type_passthrough(src_mtype(), dst_mtype(), true);
jrose@1145 2244 break;
jrose@1145 2245
jrose@1145 2246 case _adapter_check_cast:
jrose@1145 2247 {
jrose@1145 2248 // The actual value being checked must be a reference:
jrose@2639 2249 err = check_argument_type_change(java_lang_invoke_MethodType::ptype(src_mtype(), argnum),
jrose@1145 2250 object_java_mirror(), argnum);
jrose@1145 2251 if (err != NULL) break;
jrose@1145 2252
jrose@1145 2253 // The output of the cast must fit with the destination argument:
jrose@1145 2254 Handle cast_class = argument;
jrose@1145 2255 err = check_method_type_conversion(src_mtype(),
jrose@1145 2256 argnum, cast_class(),
jrose@1145 2257 dst_mtype());
jrose@1145 2258 }
jrose@1145 2259 break;
jrose@1145 2260
jrose@1145 2261 // %%% TO DO: continue in remaining cases to verify src/dst_mtype if VerifyMethodHandles
jrose@1145 2262 }
jrose@1145 2263 }
jrose@1145 2264
jrose@1145 2265 if (err != NULL) {
jrose@1145 2266 throw_InternalError_for_bad_conversion(conversion, err, THREAD);
jrose@1145 2267 return;
jrose@1145 2268 }
jrose@1145 2269
jrose@1145 2270 }
jrose@1145 2271
jrose@1145 2272 void MethodHandles::init_AdapterMethodHandle(Handle mh, Handle target, int argnum, TRAPS) {
twisti@2903 2273 Handle argument = java_lang_invoke_AdapterMethodHandle::argument(mh());
twisti@2903 2274 int argslot = java_lang_invoke_AdapterMethodHandle::vmargslot(mh());
twisti@2903 2275 jint conversion = java_lang_invoke_AdapterMethodHandle::conversion(mh());
twisti@2903 2276 jint conv_op = adapter_conversion_op(conversion);
jrose@1145 2277
jrose@1145 2278 // adjust the adapter code to the internal EntryKind enumeration:
jrose@1145 2279 EntryKind ek_orig = adapter_entry_kind(conv_op);
jrose@1145 2280 EntryKind ek_opt = ek_orig; // may be optimized
never@2895 2281 EntryKind ek_try; // temp
jrose@1145 2282
jrose@1145 2283 // Finalize the vmtarget field (Java initialized it to null).
jrose@2639 2284 if (!java_lang_invoke_MethodHandle::is_instance(target())) {
jrose@1145 2285 throw_InternalError_for_bad_conversion(conversion, "bad target", THREAD);
jrose@1145 2286 return;
jrose@1145 2287 }
jrose@2639 2288 java_lang_invoke_AdapterMethodHandle::set_vmtarget(mh(), target());
jrose@1145 2289
jrose@1145 2290 int stack_move = adapter_conversion_stack_move(conversion);
jrose@1145 2291 BasicType src = adapter_conversion_src_type(conversion);
jrose@1145 2292 BasicType dest = adapter_conversion_dest_type(conversion);
jrose@1145 2293 int vminfo = adapter_conversion_vminfo(conversion); // should be zero
jrose@1145 2294
never@2895 2295 int slots_pushed = stack_move / stack_move_unit();
never@2895 2296
never@2895 2297 if (VerifyMethodHandles) {
never@2895 2298 verify_AdapterMethodHandle(mh, argnum, CHECK);
never@2895 2299 }
never@2895 2300
jrose@1145 2301 const char* err = NULL;
jrose@1145 2302
never@2895 2303 if (!conv_op_supported(conv_op)) {
never@2895 2304 err = "adapter not yet implemented in the JVM";
never@2895 2305 }
never@2895 2306
jrose@1145 2307 // Now it's time to finish the case analysis and pick a MethodHandleEntry.
jrose@1145 2308 switch (ek_orig) {
jrose@1145 2309 case _adapter_retype_only:
jrose@1474 2310 case _adapter_retype_raw:
jrose@1145 2311 case _adapter_check_cast:
jrose@1145 2312 case _adapter_dup_args:
jrose@1145 2313 case _adapter_drop_args:
jrose@1145 2314 // these work fine via general case code
jrose@1145 2315 break;
jrose@1145 2316
jrose@1145 2317 case _adapter_prim_to_prim:
jrose@1145 2318 {
jrose@1145 2319 // Non-subword cases are {int,float,long,double} -> {int,float,long,double}.
jrose@1145 2320 // And, the {float,double} -> {int,long} cases must be handled by Java.
jrose@1145 2321 switch (type2size[src] *4+ type2size[dest]) {
jrose@1145 2322 case 1 *4+ 1:
jrose@1145 2323 assert(src == T_INT || is_subword_type(src), "source is not float");
jrose@1145 2324 // Subword-related cases are int -> {boolean,byte,char,short}.
jrose@1145 2325 ek_opt = _adapter_opt_i2i;
twisti@2204 2326 vminfo = adapter_prim_to_prim_subword_vminfo(dest);
jrose@1145 2327 break;
jrose@1145 2328 case 2 *4+ 1:
jrose@1145 2329 if (src == T_LONG && (dest == T_INT || is_subword_type(dest))) {
jrose@1145 2330 ek_opt = _adapter_opt_l2i;
twisti@2204 2331 vminfo = adapter_prim_to_prim_subword_vminfo(dest);
jrose@1145 2332 } else if (src == T_DOUBLE && dest == T_FLOAT) {
jrose@1145 2333 ek_opt = _adapter_opt_d2f;
jrose@1145 2334 } else {
twisti@2903 2335 goto throw_not_impl; // runs user code, hence could block
jrose@1145 2336 }
jrose@1145 2337 break;
jrose@1145 2338 case 1 *4+ 2:
never@2895 2339 if ((src == T_INT || is_subword_type(src)) && dest == T_LONG) {
jrose@1145 2340 ek_opt = _adapter_opt_i2l;
jrose@1145 2341 } else if (src == T_FLOAT && dest == T_DOUBLE) {
jrose@1145 2342 ek_opt = _adapter_opt_f2d;
jrose@1145 2343 } else {
twisti@2903 2344 goto throw_not_impl; // runs user code, hence could block
jrose@1145 2345 }
jrose@1145 2346 break;
jrose@1145 2347 default:
twisti@2903 2348 goto throw_not_impl; // runs user code, hence could block
jrose@1145 2349 break;
jrose@1145 2350 }
jrose@1145 2351 }
jrose@1145 2352 break;
jrose@1145 2353
jrose@1145 2354 case _adapter_ref_to_prim:
jrose@1145 2355 {
jrose@1145 2356 switch (type2size[dest]) {
jrose@1145 2357 case 1:
jrose@1145 2358 ek_opt = _adapter_opt_unboxi;
twisti@2204 2359 vminfo = adapter_unbox_subword_vminfo(dest);
jrose@1145 2360 break;
jrose@1145 2361 case 2:
jrose@1145 2362 ek_opt = _adapter_opt_unboxl;
jrose@1145 2363 break;
jrose@1145 2364 default:
twisti@2903 2365 goto throw_not_impl;
jrose@1145 2366 break;
jrose@1145 2367 }
jrose@1145 2368 }
jrose@1145 2369 break;
jrose@1145 2370
jrose@1145 2371 case _adapter_prim_to_ref:
never@2895 2372 {
never@2895 2373 assert(UseRicochetFrames, "else don't come here");
never@2895 2374 // vminfo will be the location to insert the return value
never@2895 2375 vminfo = argslot;
never@2895 2376 ek_opt = _adapter_opt_collect_ref;
never@2895 2377 ensure_vmlayout_field(target, CHECK);
twisti@2903 2378 // for MethodHandleWalk:
twisti@2903 2379 if (java_lang_invoke_AdapterMethodHandle::is_instance(argument()))
twisti@2903 2380 ensure_vmlayout_field(argument, CHECK);
never@2895 2381 if (!OptimizeMethodHandles) break;
never@2895 2382 switch (type2size[src]) {
never@2895 2383 case 1:
never@2895 2384 ek_try = EntryKind(_adapter_opt_filter_S0_ref + argslot);
never@2895 2385 if (ek_try < _adapter_opt_collect_LAST &&
never@2895 2386 ek_adapter_opt_collect_slot(ek_try) == argslot) {
never@2895 2387 assert(ek_adapter_opt_collect_count(ek_try) == 1 &&
never@2895 2388 ek_adapter_opt_collect_type(ek_try) == T_OBJECT, "");
never@2895 2389 ek_opt = ek_try;
never@2895 2390 break;
never@2895 2391 }
never@2895 2392 // else downgrade to variable slot:
never@2895 2393 ek_opt = _adapter_opt_collect_1_ref;
never@2895 2394 break;
never@2895 2395 case 2:
never@2895 2396 ek_try = EntryKind(_adapter_opt_collect_2_S0_ref + argslot);
never@2895 2397 if (ek_try < _adapter_opt_collect_LAST &&
never@2895 2398 ek_adapter_opt_collect_slot(ek_try) == argslot) {
never@2895 2399 assert(ek_adapter_opt_collect_count(ek_try) == 2 &&
never@2895 2400 ek_adapter_opt_collect_type(ek_try) == T_OBJECT, "");
never@2895 2401 ek_opt = ek_try;
never@2895 2402 break;
never@2895 2403 }
never@2895 2404 // else downgrade to variable slot:
never@2895 2405 ek_opt = _adapter_opt_collect_2_ref;
never@2895 2406 break;
never@2895 2407 default:
twisti@2903 2408 goto throw_not_impl;
never@2895 2409 break;
never@2895 2410 }
never@2895 2411 }
never@2895 2412 break;
jrose@1145 2413
jrose@1145 2414 case _adapter_swap_args:
jrose@1145 2415 case _adapter_rot_args:
jrose@1145 2416 {
jrose@1145 2417 int swap_slots = type2size[src];
jrose@1145 2418 int src_slot = argslot;
jrose@1145 2419 int dest_slot = vminfo;
jrose@1145 2420 int rotate = (ek_orig == _adapter_swap_args) ? 0 : (src_slot > dest_slot) ? 1 : -1;
jrose@1145 2421 switch (swap_slots) {
jrose@1145 2422 case 1:
jrose@1145 2423 ek_opt = (!rotate ? _adapter_opt_swap_1 :
jrose@1145 2424 rotate > 0 ? _adapter_opt_rot_1_up : _adapter_opt_rot_1_down);
jrose@1145 2425 break;
jrose@1145 2426 case 2:
jrose@1145 2427 ek_opt = (!rotate ? _adapter_opt_swap_2 :
jrose@1145 2428 rotate > 0 ? _adapter_opt_rot_2_up : _adapter_opt_rot_2_down);
jrose@1145 2429 break;
jrose@1145 2430 default:
twisti@2903 2431 goto throw_not_impl;
jrose@1145 2432 break;
jrose@1145 2433 }
jrose@1145 2434 }
jrose@1145 2435 break;
jrose@1145 2436
jrose@1145 2437 case _adapter_spread_args:
jrose@1145 2438 {
never@2895 2439 #ifdef TARGET_ARCH_NYI_6939861
never@2895 2440 // ports before 6939861 supported only three kinds of spread ops
never@2895 2441 if (!UseRicochetFrames) {
never@2895 2442 int array_size = slots_pushed + 1;
never@2895 2443 assert(array_size >= 0, "");
never@2895 2444 vminfo = array_size;
never@2895 2445 switch (array_size) {
never@2895 2446 case 0: ek_opt = _adapter_opt_spread_0; break;
never@2895 2447 case 1: ek_opt = _adapter_opt_spread_1; break;
never@2895 2448 default: ek_opt = _adapter_opt_spread_more; break;
never@2895 2449 }
never@2895 2450 break;
never@2895 2451 }
never@2895 2452 #endif //TARGET_ARCH_NYI_6939861
jrose@1145 2453 // vminfo will be the required length of the array
never@2895 2454 int array_size = (slots_pushed + 1) / (type2size[dest] == 2 ? 2 : 1);
jrose@1145 2455 vminfo = array_size;
never@2895 2456 // general case
never@2895 2457 switch (dest) {
never@2895 2458 case T_BOOLEAN : // fall through to T_BYTE:
never@2895 2459 case T_BYTE : ek_opt = _adapter_opt_spread_byte; break;
never@2895 2460 case T_CHAR : ek_opt = _adapter_opt_spread_char; break;
never@2895 2461 case T_SHORT : ek_opt = _adapter_opt_spread_short; break;
never@2895 2462 case T_INT : ek_opt = _adapter_opt_spread_int; break;
never@2895 2463 case T_LONG : ek_opt = _adapter_opt_spread_long; break;
never@2895 2464 case T_FLOAT : ek_opt = _adapter_opt_spread_float; break;
never@2895 2465 case T_DOUBLE : ek_opt = _adapter_opt_spread_double; break;
never@2895 2466 case T_OBJECT : ek_opt = _adapter_opt_spread_ref; break;
never@2895 2467 case T_VOID : if (array_size != 0) goto throw_not_impl;
never@2895 2468 ek_opt = _adapter_opt_spread_ref; break;
never@2895 2469 default : goto throw_not_impl;
jrose@1145 2470 }
never@2895 2471 assert(array_size == 0 || // it doesn't matter what the spreader is
never@2895 2472 (ek_adapter_opt_spread_count(ek_opt) == -1 &&
never@2895 2473 (ek_adapter_opt_spread_type(ek_opt) == dest ||
never@2895 2474 (ek_adapter_opt_spread_type(ek_opt) == T_BYTE && dest == T_BOOLEAN))),
never@2895 2475 err_msg("dest=%d ek_opt=%d", dest, ek_opt));
never@2895 2476
never@2895 2477 if (array_size <= 0) {
never@2895 2478 // since the general case does not handle length 0, this case is required:
never@2895 2479 ek_opt = _adapter_opt_spread_0;
never@2895 2480 break;
never@2895 2481 }
never@2895 2482 if (dest == T_OBJECT) {
never@2895 2483 ek_try = EntryKind(_adapter_opt_spread_1_ref - 1 + array_size);
never@2895 2484 if (ek_try < _adapter_opt_spread_LAST &&
never@2895 2485 ek_adapter_opt_spread_count(ek_try) == array_size) {
never@2895 2486 assert(ek_adapter_opt_spread_type(ek_try) == dest, "");
never@2895 2487 ek_opt = ek_try;
never@2895 2488 break;
never@2895 2489 }
never@2895 2490 }
never@2895 2491 break;
jrose@1145 2492 }
jrose@1145 2493 break;
jrose@1145 2494
never@2895 2495 case _adapter_collect_args:
never@2895 2496 {
never@2895 2497 assert(UseRicochetFrames, "else don't come here");
twisti@2903 2498 int elem_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(argument()));
never@2895 2499 // vminfo will be the location to insert the return value
never@2895 2500 vminfo = argslot;
never@2895 2501 ensure_vmlayout_field(target, CHECK);
twisti@2903 2502 ensure_vmlayout_field(argument, CHECK);
never@2895 2503
never@2895 2504 // general case:
never@2895 2505 switch (dest) {
never@2895 2506 default : if (!is_subword_type(dest)) goto throw_not_impl;
never@2895 2507 // else fall through:
never@2895 2508 case T_INT : ek_opt = _adapter_opt_collect_int; break;
never@2895 2509 case T_LONG : ek_opt = _adapter_opt_collect_long; break;
never@2895 2510 case T_FLOAT : ek_opt = _adapter_opt_collect_float; break;
never@2895 2511 case T_DOUBLE : ek_opt = _adapter_opt_collect_double; break;
never@2895 2512 case T_OBJECT : ek_opt = _adapter_opt_collect_ref; break;
never@2895 2513 case T_VOID : ek_opt = _adapter_opt_collect_void; break;
never@2895 2514 }
never@2895 2515 assert(ek_adapter_opt_collect_slot(ek_opt) == -1 &&
never@2895 2516 ek_adapter_opt_collect_count(ek_opt) == -1 &&
never@2895 2517 (ek_adapter_opt_collect_type(ek_opt) == dest ||
never@2895 2518 ek_adapter_opt_collect_type(ek_opt) == T_INT && is_subword_type(dest)),
never@2895 2519 "");
never@2895 2520
never@2895 2521 if (dest == T_OBJECT && elem_slots == 1 && OptimizeMethodHandles) {
never@2895 2522 // filter operation on a ref
never@2895 2523 ek_try = EntryKind(_adapter_opt_filter_S0_ref + argslot);
never@2895 2524 if (ek_try < _adapter_opt_collect_LAST &&
never@2895 2525 ek_adapter_opt_collect_slot(ek_try) == argslot) {
never@2895 2526 assert(ek_adapter_opt_collect_count(ek_try) == elem_slots &&
never@2895 2527 ek_adapter_opt_collect_type(ek_try) == dest, "");
never@2895 2528 ek_opt = ek_try;
never@2895 2529 break;
never@2895 2530 }
never@2895 2531 ek_opt = _adapter_opt_collect_1_ref;
never@2895 2532 break;
never@2895 2533 }
never@2895 2534
never@2895 2535 if (dest == T_OBJECT && elem_slots == 2 && OptimizeMethodHandles) {
never@2895 2536 // filter of two arguments
never@2895 2537 ek_try = EntryKind(_adapter_opt_collect_2_S0_ref + argslot);
never@2895 2538 if (ek_try < _adapter_opt_collect_LAST &&
never@2895 2539 ek_adapter_opt_collect_slot(ek_try) == argslot) {
never@2895 2540 assert(ek_adapter_opt_collect_count(ek_try) == elem_slots &&
never@2895 2541 ek_adapter_opt_collect_type(ek_try) == dest, "");
never@2895 2542 ek_opt = ek_try;
never@2895 2543 break;
never@2895 2544 }
never@2895 2545 ek_opt = _adapter_opt_collect_2_ref;
never@2895 2546 break;
never@2895 2547 }
never@2895 2548
never@2895 2549 if (dest == T_OBJECT && OptimizeMethodHandles) {
never@2895 2550 // try to use a fixed length adapter
never@2895 2551 ek_try = EntryKind(_adapter_opt_collect_0_ref + elem_slots);
never@2895 2552 if (ek_try < _adapter_opt_collect_LAST &&
never@2895 2553 ek_adapter_opt_collect_count(ek_try) == elem_slots) {
never@2895 2554 assert(ek_adapter_opt_collect_slot(ek_try) == -1 &&
never@2895 2555 ek_adapter_opt_collect_type(ek_try) == dest, "");
never@2895 2556 ek_opt = ek_try;
never@2895 2557 break;
never@2895 2558 }
never@2895 2559 }
never@2895 2560
never@2895 2561 break;
never@2895 2562 }
never@2895 2563
never@2895 2564 case _adapter_fold_args:
never@2895 2565 {
never@2895 2566 assert(UseRicochetFrames, "else don't come here");
twisti@2903 2567 int elem_slots = argument_slot_count(java_lang_invoke_MethodHandle::type(argument()));
never@2895 2568 // vminfo will be the location to insert the return value
never@2895 2569 vminfo = argslot + elem_slots;
never@2895 2570 ensure_vmlayout_field(target, CHECK);
twisti@2903 2571 ensure_vmlayout_field(argument, CHECK);
never@2895 2572
never@2895 2573 switch (dest) {
never@2895 2574 default : if (!is_subword_type(dest)) goto throw_not_impl;
never@2895 2575 // else fall through:
never@2895 2576 case T_INT : ek_opt = _adapter_opt_fold_int; break;
never@2895 2577 case T_LONG : ek_opt = _adapter_opt_fold_long; break;
never@2895 2578 case T_FLOAT : ek_opt = _adapter_opt_fold_float; break;
never@2895 2579 case T_DOUBLE : ek_opt = _adapter_opt_fold_double; break;
never@2895 2580 case T_OBJECT : ek_opt = _adapter_opt_fold_ref; break;
never@2895 2581 case T_VOID : ek_opt = _adapter_opt_fold_void; break;
never@2895 2582 }
never@2895 2583 assert(ek_adapter_opt_collect_slot(ek_opt) == -1 &&
never@2895 2584 ek_adapter_opt_collect_count(ek_opt) == -1 &&
never@2895 2585 (ek_adapter_opt_collect_type(ek_opt) == dest ||
never@2895 2586 ek_adapter_opt_collect_type(ek_opt) == T_INT && is_subword_type(dest)),
never@2895 2587 "");
never@2895 2588
never@2895 2589 if (dest == T_OBJECT && elem_slots == 0 && OptimizeMethodHandles) {
never@2895 2590 // if there are no args, just pretend it's a collect
never@2895 2591 ek_opt = _adapter_opt_collect_0_ref;
never@2895 2592 break;
never@2895 2593 }
never@2895 2594
never@2895 2595 if (dest == T_OBJECT && OptimizeMethodHandles) {
never@2895 2596 // try to use a fixed length adapter
never@2895 2597 ek_try = EntryKind(_adapter_opt_fold_1_ref - 1 + elem_slots);
never@2895 2598 if (ek_try < _adapter_opt_fold_LAST &&
never@2895 2599 ek_adapter_opt_collect_count(ek_try) == elem_slots) {
never@2895 2600 assert(ek_adapter_opt_collect_slot(ek_try) == -1 &&
never@2895 2601 ek_adapter_opt_collect_type(ek_try) == dest, "");
never@2895 2602 ek_opt = ek_try;
never@2895 2603 break;
never@2895 2604 }
never@2895 2605 }
never@2895 2606
never@2895 2607 break;
never@2895 2608 }
jrose@1145 2609
jrose@1145 2610 default:
jrose@1145 2611 // should have failed much earlier; must be a missing case here
jrose@1145 2612 assert(false, "incomplete switch");
jrose@1145 2613 // and fall through:
jrose@1145 2614
jrose@1145 2615 throw_not_impl:
never@2895 2616 if (err == NULL)
never@2895 2617 err = "unknown adapter type";
jrose@1145 2618 break;
jrose@1145 2619 }
jrose@1145 2620
twisti@2903 2621 if (err == NULL && (vminfo & CONV_VMINFO_MASK) != vminfo) {
never@2895 2622 // should not happen, since vminfo is used to encode arg/slot indexes < 255
never@2895 2623 err = "vminfo overflow";
never@2895 2624 }
never@2895 2625
twisti@2903 2626 if (err == NULL && !have_entry(ek_opt)) {
never@2895 2627 err = "adapter stub for this kind of method handle is missing";
never@2895 2628 }
never@2895 2629
twisti@2903 2630 if (err == NULL && ek_opt == ek_orig) {
twisti@2903 2631 switch (ek_opt) {
twisti@2903 2632 case _adapter_prim_to_prim:
twisti@2903 2633 case _adapter_ref_to_prim:
twisti@2903 2634 case _adapter_prim_to_ref:
twisti@2903 2635 case _adapter_swap_args:
twisti@2903 2636 case _adapter_rot_args:
twisti@2903 2637 case _adapter_collect_args:
twisti@2903 2638 case _adapter_fold_args:
twisti@2903 2639 case _adapter_spread_args:
twisti@2903 2640 // should be handled completely by optimized cases; see above
twisti@2903 2641 err = "init_AdapterMethodHandle should not issue this";
twisti@2903 2642 break;
twisti@2903 2643 }
twisti@2903 2644 }
twisti@2903 2645
jrose@1145 2646 if (err != NULL) {
never@2950 2647 throw_InternalError_for_bad_conversion(conversion, err_msg("%s: conv_op %d ek_opt %d", err, conv_op, ek_opt), THREAD);
jrose@1145 2648 return;
jrose@1145 2649 }
jrose@1145 2650
jrose@1145 2651 // Rebuild the conversion value; maybe parts of it were changed.
jrose@1145 2652 jint new_conversion = adapter_conversion(conv_op, src, dest, stack_move, vminfo);
jrose@1145 2653
jrose@1145 2654 // Finalize the conversion field. (Note that it is final to Java code.)
jrose@2639 2655 java_lang_invoke_AdapterMethodHandle::set_conversion(mh(), new_conversion);
jrose@1145 2656
never@3105 2657 if (java_lang_invoke_CountingMethodHandle::is_instance(mh())) {
never@3105 2658 assert(ek_orig == _adapter_retype_only, "only one handled");
never@3105 2659 ek_opt = _adapter_opt_profiling;
never@3105 2660 }
never@3105 2661
jrose@1145 2662 // Done!
jrose@2639 2663 java_lang_invoke_MethodHandle::set_vmentry(mh(), entry(ek_opt));
jrose@1145 2664
jrose@1145 2665 // There should be enough memory barriers on exit from native methods
jrose@1145 2666 // to ensure that the MH is fully initialized to all threads before
jrose@1145 2667 // Java code can publish it in global data structures.
jrose@1145 2668 }
jrose@1145 2669
never@2895 2670 void MethodHandles::ensure_vmlayout_field(Handle target, TRAPS) {
never@2895 2671 Handle mtype(THREAD, java_lang_invoke_MethodHandle::type(target()));
never@2895 2672 Handle mtform(THREAD, java_lang_invoke_MethodType::form(mtype()));
never@2895 2673 if (mtform.is_null()) { THROW(vmSymbols::java_lang_InternalError()); }
never@2895 2674 if (java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() > 0) {
never@2895 2675 if (java_lang_invoke_MethodTypeForm::vmlayout(mtform()) == NULL) {
never@2895 2676 // fill it in
never@2895 2677 Handle erased_mtype(THREAD, java_lang_invoke_MethodTypeForm::erasedType(mtform()));
never@2895 2678 TempNewSymbol erased_signature
never@2895 2679 = java_lang_invoke_MethodType::as_signature(erased_mtype(), /*intern:*/true, CHECK);
never@2895 2680 methodOop cookie
never@2895 2681 = SystemDictionary::find_method_handle_invoke(vmSymbols::invokeExact_name(),
never@2895 2682 erased_signature,
never@2895 2683 SystemDictionaryHandles::Object_klass(),
never@2895 2684 THREAD);
never@2895 2685 java_lang_invoke_MethodTypeForm::init_vmlayout(mtform(), cookie);
never@2895 2686 }
never@2895 2687 }
never@3137 2688 assert(java_lang_invoke_MethodTypeForm::vmslots(mtform()) == argument_slot_count(mtype()), "must agree");
never@2895 2689 }
never@2895 2690
never@2920 2691 #ifdef ASSERT
never@2920 2692
never@2920 2693 extern "C"
never@2920 2694 void print_method_handle(oop mh);
never@2920 2695
never@2920 2696 static void stress_method_handle_walk_impl(Handle mh, TRAPS) {
never@2920 2697 if (StressMethodHandleWalk) {
never@2920 2698 // Exercise the MethodHandleWalk code in various ways and validate
never@2920 2699 // the resulting method oop. Some of these produce output so they
never@2920 2700 // are guarded under Verbose.
never@2920 2701 ResourceMark rm;
never@2920 2702 HandleMark hm;
never@2920 2703 if (Verbose) {
never@2920 2704 print_method_handle(mh());
never@2920 2705 }
never@2920 2706 TempNewSymbol name = SymbolTable::new_symbol("invoke", CHECK);
never@2920 2707 Handle mt = java_lang_invoke_MethodHandle::type(mh());
never@2920 2708 TempNewSymbol signature = java_lang_invoke_MethodType::as_signature(mt(), true, CHECK);
never@2920 2709 MethodHandleCompiler mhc(mh, name, signature, 10000, false, CHECK);
never@2920 2710 methodHandle m = mhc.compile(CHECK);
never@2920 2711 if (Verbose) {
never@2920 2712 m->print_codes();
never@2920 2713 }
never@2920 2714 InterpreterOopMap mask;
never@2920 2715 OopMapCache::compute_one_oop_map(m, m->code_size() - 1, &mask);
jrose@2982 2716 // compile to object code if -Xcomp or WizardMode
jrose@2982 2717 if ((WizardMode ||
jrose@2982 2718 CompilationPolicy::must_be_compiled(m))
jrose@2982 2719 && !instanceKlass::cast(m->method_holder())->is_not_initialized()
jrose@2982 2720 && CompilationPolicy::can_be_compiled(m)) {
jrose@2982 2721 // Force compilation
jrose@2982 2722 CompileBroker::compile_method(m, InvocationEntryBci,
iveresov@3035 2723 CompilationPolicy::policy()->initial_compile_level(),
jrose@2982 2724 methodHandle(), 0, "StressMethodHandleWalk",
jrose@2982 2725 CHECK);
jrose@2982 2726 }
never@2920 2727 }
never@2920 2728 }
never@2920 2729
never@2920 2730 static void stress_method_handle_walk(Handle mh, TRAPS) {
never@2920 2731 stress_method_handle_walk_impl(mh, THREAD);
never@2920 2732 if (HAS_PENDING_EXCEPTION) {
never@2920 2733 oop ex = PENDING_EXCEPTION;
never@2920 2734 CLEAR_PENDING_EXCEPTION;
never@2920 2735 tty->print("StressMethodHandleWalk: ");
never@2920 2736 java_lang_Throwable::print(ex, tty);
never@2920 2737 tty->cr();
never@2920 2738 }
never@2920 2739 }
never@2920 2740 #else
never@2920 2741
never@2920 2742 static void stress_method_handle_walk(Handle mh, TRAPS) {}
never@2920 2743
never@2920 2744 #endif
never@2920 2745
jrose@1145 2746 //
jrose@2639 2747 // Here are the native methods on sun.invoke.MethodHandleImpl.
jrose@1145 2748 // They are the private interface between this JVM and the HotSpot-specific
jrose@1145 2749 // Java code that implements JSR 292 method handles.
jrose@1145 2750 //
jrose@1145 2751 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way
jrose@1145 2752 // that intrinsic (non-JNI) native methods are defined in HotSpot.
jrose@1145 2753 //
jrose@1145 2754
jrose@1145 2755 // direct method handles for invokestatic or invokespecial
jrose@1145 2756 // void init(DirectMethodHandle self, MemberName ref, boolean doDispatch, Class<?> caller);
jrose@2639 2757 JVM_ENTRY(void, MHN_init_DMH(JNIEnv *env, jobject igcls, jobject mh_jh,
jrose@1145 2758 jobject target_jh, jboolean do_dispatch, jobject caller_jh)) {
jrose@1145 2759 ResourceMark rm; // for error messages
jrose@1145 2760
jrose@1145 2761 // This is the guy we are initializing:
never@2937 2762 if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); }
jrose@1145 2763 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
jrose@1145 2764
jrose@1145 2765 // Early returns out of this method leave the DMH in an unfinished state.
jrose@2639 2766 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
jrose@1145 2767
jrose@1145 2768 // which method are we really talking about?
never@2937 2769 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
twisti@2806 2770 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
twisti@2806 2771 if (java_lang_invoke_MemberName::is_instance(target()) &&
twisti@2806 2772 java_lang_invoke_MemberName::vmindex(target()) == VM_INDEX_UNINITIALIZED) {
twisti@2806 2773 MethodHandles::resolve_MemberName(target, CHECK);
jrose@1145 2774 }
jrose@1145 2775
twisti@2806 2776 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 2777 methodHandle m = MethodHandles::decode_method(target(), receiver_limit, decode_flags);
jrose@1145 2778 if (m.is_null()) { THROW_MSG(vmSymbols::java_lang_InternalError(), "no such method"); }
jrose@1145 2779
jrose@1145 2780 // The trusted Java code that calls this method should already have performed
jrose@1145 2781 // access checks on behalf of the given caller. But, we can verify this.
jrose@1145 2782 if (VerifyMethodHandles && caller_jh != NULL) {
jrose@1145 2783 KlassHandle caller(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh)));
jrose@1145 2784 // If this were a bytecode, the first access check would be against
jrose@1145 2785 // the "reference class" mentioned in the CONSTANT_Methodref.
jrose@1862 2786 // We don't know at this point which class that was, and if we
jrose@1862 2787 // check against m.method_holder we might get the wrong answer.
jrose@1862 2788 // So we just make sure to handle this check when the resolution
jrose@1862 2789 // happens, when we call resolve_MemberName.
jrose@1862 2790 //
jrose@1862 2791 // (A public class can inherit public members from private supers,
jrose@1862 2792 // and it would be wrong to check access against the private super
jrose@1862 2793 // if the original symbolic reference was against the public class.)
jrose@1862 2794 //
jrose@1145 2795 // If there were a bytecode, the next step would be to lookup the method
jrose@1145 2796 // in the reference class, then then check the method's access bits.
jrose@1145 2797 // Emulate LinkResolver::check_method_accessability.
jrose@1145 2798 klassOop resolved_klass = m->method_holder();
jrose@1145 2799 if (!Reflection::verify_field_access(caller->as_klassOop(),
jrose@1862 2800 resolved_klass, resolved_klass,
jrose@1145 2801 m->access_flags(),
jrose@1145 2802 true)) {
jrose@1145 2803 // %%% following cutout belongs in Reflection::verify_field_access?
jrose@1145 2804 bool same_pm = Reflection::is_same_package_member(caller->as_klassOop(),
jrose@1862 2805 resolved_klass, THREAD);
jrose@1145 2806 if (!same_pm) {
jrose@1145 2807 THROW_MSG(vmSymbols::java_lang_InternalError(), m->name_and_sig_as_C_string());
jrose@1145 2808 }
jrose@1145 2809 }
jrose@1145 2810 }
jrose@1145 2811
jrose@1145 2812 MethodHandles::init_DirectMethodHandle(mh, m, (do_dispatch != JNI_FALSE), CHECK);
never@2920 2813 stress_method_handle_walk(mh, CHECK);
jrose@1145 2814 }
jrose@1145 2815 JVM_END
jrose@1145 2816
jrose@1145 2817 // bound method handles
jrose@2639 2818 JVM_ENTRY(void, MHN_init_BMH(JNIEnv *env, jobject igcls, jobject mh_jh,
jrose@1145 2819 jobject target_jh, int argnum)) {
jrose@1145 2820 ResourceMark rm; // for error messages
jrose@1145 2821
jrose@1145 2822 // This is the guy we are initializing:
never@2937 2823 if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); }
jrose@1145 2824 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
jrose@1145 2825
jrose@1145 2826 // Early returns out of this method leave the BMH in an unfinished state.
jrose@2639 2827 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
jrose@1145 2828
never@2937 2829 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
jrose@1145 2830 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
jrose@1145 2831
jrose@2639 2832 if (!java_lang_invoke_MethodHandle::is_instance(target())) {
jrose@1145 2833 // Target object is a reflective method. (%%% Do we need this alternate path?)
jrose@1145 2834 Untested("init_BMH of non-MH");
jrose@1145 2835 if (argnum != 0) { THROW(vmSymbols::java_lang_InternalError()); }
twisti@2806 2836 KlassHandle receiver_limit; int decode_flags = 0;
twisti@2806 2837 methodHandle m = MethodHandles::decode_method(target(), receiver_limit, decode_flags);
jrose@1145 2838 MethodHandles::init_BoundMethodHandle_with_receiver(mh, m,
jrose@1145 2839 receiver_limit,
jrose@1145 2840 decode_flags,
jrose@1145 2841 CHECK);
never@2920 2842 } else {
never@2920 2843 // Build a BMH on top of a DMH or another BMH:
never@2920 2844 MethodHandles::init_BoundMethodHandle(mh, target, argnum, CHECK);
jrose@1145 2845 }
jrose@2982 2846
jrose@2982 2847 if (StressMethodHandleWalk) {
jrose@2982 2848 if (mh->klass() == SystemDictionary::BoundMethodHandle_klass())
jrose@2982 2849 stress_method_handle_walk(mh, CHECK);
jrose@2982 2850 // else don't, since the subclass has not yet initialized its own fields
jrose@2982 2851 }
jrose@1145 2852 }
jrose@1145 2853 JVM_END
jrose@1145 2854
jrose@1145 2855 // adapter method handles
jrose@2639 2856 JVM_ENTRY(void, MHN_init_AMH(JNIEnv *env, jobject igcls, jobject mh_jh,
jrose@1145 2857 jobject target_jh, int argnum)) {
jrose@1145 2858 // This is the guy we are initializing:
never@2937 2859 if (mh_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "self is null"); }
never@2937 2860 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
jrose@1145 2861 Handle mh(THREAD, JNIHandles::resolve_non_null(mh_jh));
jrose@1145 2862 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
jrose@1145 2863
jrose@1145 2864 // Early returns out of this method leave the AMH in an unfinished state.
jrose@2639 2865 assert(java_lang_invoke_MethodHandle::vmentry(mh()) == NULL, "must be safely null");
jrose@1145 2866
jrose@1145 2867 MethodHandles::init_AdapterMethodHandle(mh, target, argnum, CHECK);
never@2920 2868 stress_method_handle_walk(mh, CHECK);
jrose@1145 2869 }
jrose@1145 2870 JVM_END
jrose@1145 2871
jrose@1145 2872 // method type forms
jrose@2639 2873 JVM_ENTRY(void, MHN_init_MT(JNIEnv *env, jobject igcls, jobject erased_jh)) {
jrose@1145 2874 if (erased_jh == NULL) return;
jrose@1145 2875 if (TraceMethodHandles) {
jrose@1145 2876 tty->print("creating MethodType form ");
jrose@1145 2877 if (WizardMode || Verbose) { // Warning: this calls Java code on the MH!
jrose@1145 2878 // call Object.toString()
coleenp@2497 2879 Symbol* name = vmSymbols::toString_name();
coleenp@2497 2880 Symbol* sig = vmSymbols::void_string_signature();
jrose@1145 2881 JavaCallArguments args(Handle(THREAD, JNIHandles::resolve_non_null(erased_jh)));
jrose@1145 2882 JavaValue result(T_OBJECT);
never@1577 2883 JavaCalls::call_virtual(&result, SystemDictionary::Object_klass(), name, sig,
jrose@1145 2884 &args, CHECK);
jrose@1145 2885 Handle str(THREAD, (oop)result.get_jobject());
jrose@1145 2886 java_lang_String::print(str, tty);
jrose@1145 2887 }
jrose@1145 2888 tty->cr();
jrose@1145 2889 }
jrose@1145 2890 }
jrose@1145 2891 JVM_END
jrose@1145 2892
jrose@1145 2893 // debugging and reflection
jrose@2639 2894 JVM_ENTRY(jobject, MHN_getTarget(JNIEnv *env, jobject igcls, jobject mh_jh, jint format)) {
jrose@1145 2895 Handle mh(THREAD, JNIHandles::resolve(mh_jh));
jrose@2639 2896 if (!java_lang_invoke_MethodHandle::is_instance(mh())) {
jrose@1145 2897 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
jrose@1145 2898 }
jrose@1145 2899 oop target = MethodHandles::encode_target(mh, format, CHECK_NULL);
jrose@1145 2900 return JNIHandles::make_local(THREAD, target);
jrose@1145 2901 }
jrose@1145 2902 JVM_END
jrose@1145 2903
jrose@2639 2904 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
jrose@1145 2905 switch (which) {
jrose@1145 2906 case MethodHandles::GC_JVM_PUSH_LIMIT:
jrose@1145 2907 guarantee(MethodHandlePushLimit >= 2 && MethodHandlePushLimit <= 0xFF,
jrose@1145 2908 "MethodHandlePushLimit parameter must be in valid range");
jrose@1145 2909 return MethodHandlePushLimit;
jrose@1474 2910 case MethodHandles::GC_JVM_STACK_MOVE_UNIT:
jrose@1145 2911 // return number of words per slot, signed according to stack direction
jrose@1145 2912 return MethodHandles::stack_move_unit();
jrose@1862 2913 case MethodHandles::GC_CONV_OP_IMPLEMENTED_MASK:
jrose@1862 2914 return MethodHandles::adapter_conversion_ops_supported_mask();
never@3105 2915 case MethodHandles::GC_COUNT_GWT:
never@3105 2916 #ifdef COMPILER2
never@3105 2917 return true;
never@3105 2918 #else
never@3105 2919 return false;
never@3105 2920 #endif
jrose@1145 2921 }
jrose@1145 2922 return 0;
jrose@1145 2923 }
jrose@1145 2924 JVM_END
jrose@1145 2925
jrose@1145 2926 #ifndef PRODUCT
jrose@1145 2927 #define EACH_NAMED_CON(template) \
never@2895 2928 /* hold back this one until JDK stabilizes */ \
never@2895 2929 /* template(MethodHandles,GC_JVM_PUSH_LIMIT) */ \
never@2895 2930 /* hold back this one until JDK stabilizes */ \
never@2895 2931 /* template(MethodHandles,GC_JVM_STACK_MOVE_UNIT) */ \
never@2954 2932 /* hold back this one until JDK stabilizes */ \
never@2954 2933 /* template(MethodHandles,GC_OP_ROT_ARGS_DOWN_LIMIT_BIAS) */ \
jrose@1145 2934 template(MethodHandles,ETF_HANDLE_OR_METHOD_NAME) \
jrose@1145 2935 template(MethodHandles,ETF_DIRECT_HANDLE) \
jrose@1145 2936 template(MethodHandles,ETF_METHOD_NAME) \
jrose@1145 2937 template(MethodHandles,ETF_REFLECT_METHOD) \
jrose@2639 2938 template(java_lang_invoke_MemberName,MN_IS_METHOD) \
jrose@2639 2939 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \
jrose@2639 2940 template(java_lang_invoke_MemberName,MN_IS_FIELD) \
jrose@2639 2941 template(java_lang_invoke_MemberName,MN_IS_TYPE) \
jrose@2639 2942 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
jrose@2639 2943 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
jrose@2639 2944 template(java_lang_invoke_MemberName,VM_INDEX_UNINITIALIZED) \
jrose@2639 2945 template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_ONLY) \
jrose@2639 2946 template(java_lang_invoke_AdapterMethodHandle,OP_RETYPE_RAW) \
jrose@2639 2947 template(java_lang_invoke_AdapterMethodHandle,OP_CHECK_CAST) \
jrose@2639 2948 template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_PRIM) \
jrose@2639 2949 template(java_lang_invoke_AdapterMethodHandle,OP_REF_TO_PRIM) \
jrose@2639 2950 template(java_lang_invoke_AdapterMethodHandle,OP_PRIM_TO_REF) \
jrose@2639 2951 template(java_lang_invoke_AdapterMethodHandle,OP_SWAP_ARGS) \
jrose@2639 2952 template(java_lang_invoke_AdapterMethodHandle,OP_ROT_ARGS) \
jrose@2639 2953 template(java_lang_invoke_AdapterMethodHandle,OP_DUP_ARGS) \
jrose@2639 2954 template(java_lang_invoke_AdapterMethodHandle,OP_DROP_ARGS) \
jrose@2639 2955 template(java_lang_invoke_AdapterMethodHandle,OP_COLLECT_ARGS) \
jrose@2639 2956 template(java_lang_invoke_AdapterMethodHandle,OP_SPREAD_ARGS) \
never@2895 2957 /* hold back this one until JDK stabilizes */ \
never@2895 2958 /*template(java_lang_invoke_AdapterMethodHandle,CONV_OP_LIMIT)*/ \
jrose@2639 2959 template(java_lang_invoke_AdapterMethodHandle,CONV_OP_MASK) \
jrose@2639 2960 template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_MASK) \
jrose@2639 2961 template(java_lang_invoke_AdapterMethodHandle,CONV_VMINFO_SHIFT) \
jrose@2639 2962 template(java_lang_invoke_AdapterMethodHandle,CONV_OP_SHIFT) \
jrose@2639 2963 template(java_lang_invoke_AdapterMethodHandle,CONV_DEST_TYPE_SHIFT) \
jrose@2639 2964 template(java_lang_invoke_AdapterMethodHandle,CONV_SRC_TYPE_SHIFT) \
jrose@2639 2965 template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_SHIFT) \
jrose@2639 2966 template(java_lang_invoke_AdapterMethodHandle,CONV_STACK_MOVE_MASK) \
jrose@1145 2967 /*end*/
jrose@1145 2968
jrose@1145 2969 #define ONE_PLUS(scope,value) 1+
jrose@1145 2970 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS) 0;
jrose@1145 2971 #define VALUE_COMMA(scope,value) scope::value,
jrose@1145 2972 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA) 0 };
jrose@1145 2973 #define STRING_NULL(scope,value) #value "\0"
jrose@1145 2974 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL) };
jrose@1145 2975
jrose@1145 2976 #undef ONE_PLUS
jrose@1145 2977 #undef VALUE_COMMA
jrose@1145 2978 #undef STRING_NULL
jrose@1145 2979 #undef EACH_NAMED_CON
jrose@1145 2980 #endif
jrose@1145 2981
jrose@2639 2982 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
jrose@1145 2983 #ifndef PRODUCT
jrose@1145 2984 if (which >= 0 && which < con_value_count) {
jrose@1145 2985 int con = con_values[which];
twisti@2806 2986 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
twisti@2806 2987 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) {
jrose@1145 2988 const char* str = &con_names[0];
jrose@1145 2989 for (int i = 0; i < which; i++)
jrose@1145 2990 str += strlen(str) + 1; // skip name and null
twisti@2806 2991 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
jrose@1145 2992 box->obj_at_put(0, name);
jrose@1145 2993 }
jrose@1145 2994 return con;
jrose@1145 2995 }
jrose@1145 2996 #endif
jrose@1145 2997 return 0;
jrose@1145 2998 }
jrose@1145 2999 JVM_END
jrose@1145 3000
jrose@1145 3001 // void init(MemberName self, AccessibleObject ref)
jrose@2639 3002 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
never@2937 3003 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
never@2937 3004 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
jrose@1145 3005 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
jrose@1145 3006 oop target_oop = JNIHandles::resolve_non_null(target_jh);
jrose@1145 3007 MethodHandles::init_MemberName(mname(), target_oop);
jrose@1145 3008 }
jrose@1145 3009 JVM_END
jrose@1145 3010
jrose@1145 3011 // void expand(MemberName self)
jrose@2639 3012 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
never@2937 3013 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
jrose@1145 3014 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
jrose@1145 3015 MethodHandles::expand_MemberName(mname, 0, CHECK);
jrose@1145 3016 }
jrose@1145 3017 JVM_END
jrose@1145 3018
jrose@1145 3019 // void resolve(MemberName self, Class<?> caller)
jrose@2639 3020 JVM_ENTRY(void, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) {
never@2937 3021 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
jrose@1145 3022 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
jrose@1862 3023
jrose@1862 3024 // The trusted Java code that calls this method should already have performed
jrose@1862 3025 // access checks on behalf of the given caller. But, we can verify this.
jrose@1862 3026 if (VerifyMethodHandles && caller_jh != NULL) {
jrose@2639 3027 klassOop reference_klass = java_lang_Class::as_klassOop(java_lang_invoke_MemberName::clazz(mname()));
jrose@1862 3028 if (reference_klass != NULL) {
jrose@1862 3029 // Emulate LinkResolver::check_klass_accessability.
jrose@1862 3030 klassOop caller = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(caller_jh));
jrose@1862 3031 if (!Reflection::verify_class_access(caller,
jrose@1862 3032 reference_klass,
jrose@1862 3033 true)) {
jrose@1862 3034 THROW_MSG(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name());
jrose@1862 3035 }
jrose@1862 3036 }
jrose@1862 3037 }
jrose@1862 3038
jrose@1145 3039 MethodHandles::resolve_MemberName(mname, CHECK);
jrose@1145 3040 }
jrose@1145 3041 JVM_END
jrose@1145 3042
jrose@1145 3043 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
jrose@1145 3044 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
jrose@2639 3045 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls,
jrose@1145 3046 jclass clazz_jh, jstring name_jh, jstring sig_jh,
jrose@1145 3047 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) {
jrose@1145 3048 if (clazz_jh == NULL || results_jh == NULL) return -1;
twisti@2806 3049 KlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz_jh)));
jrose@1145 3050
twisti@2806 3051 objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh));
twisti@2806 3052 if (results.is_null() || !results->is_objArray()) return -1;
jrose@1145 3053
coleenp@2497 3054 TempNewSymbol name = NULL;
coleenp@2497 3055 TempNewSymbol sig = NULL;
jrose@1145 3056 if (name_jh != NULL) {
jrose@1145 3057 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh));
jrose@1145 3058 if (name == NULL) return 0; // a match is not possible
jrose@1145 3059 }
jrose@1145 3060 if (sig_jh != NULL) {
jrose@1145 3061 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh));
jrose@1145 3062 if (sig == NULL) return 0; // a match is not possible
jrose@1145 3063 }
jrose@1145 3064
twisti@2806 3065 KlassHandle caller;
jrose@1145 3066 if (caller_jh != NULL) {
jrose@1145 3067 oop caller_oop = JNIHandles::resolve_non_null(caller_jh);
jrose@1145 3068 if (!java_lang_Class::is_instance(caller_oop)) return -1;
twisti@2806 3069 caller = KlassHandle(THREAD, java_lang_Class::as_klassOop(caller_oop));
jrose@1145 3070 }
jrose@1145 3071
twisti@2806 3072 if (name != NULL && sig != NULL && results.not_null()) {
jrose@1145 3073 // try a direct resolve
jrose@1145 3074 // %%% TO DO
jrose@1145 3075 }
jrose@1145 3076
twisti@2806 3077 int res = MethodHandles::find_MemberNames(k(), name, sig, mflags,
twisti@2806 3078 caller(), skip, results());
jrose@1145 3079 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks
jrose@1145 3080 return res;
jrose@1145 3081 }
jrose@1145 3082 JVM_END
jrose@1145 3083
twisti@3131 3084 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
twisti@3239 3085 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
twisti@3239 3086 Handle target (THREAD, JNIHandles::resolve(target_jh));
twisti@3131 3087 {
twisti@3131 3088 // Walk all nmethods depending on this call site.
twisti@3131 3089 MutexLocker mu(Compile_lock, thread);
twisti@3131 3090 Universe::flush_dependents_on(call_site, target);
twisti@3131 3091 }
twisti@3239 3092 java_lang_invoke_CallSite::set_target(call_site(), target());
twisti@3131 3093 }
twisti@3131 3094 JVM_END
twisti@3131 3095
twisti@3131 3096 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
twisti@3239 3097 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
twisti@3239 3098 Handle target (THREAD, JNIHandles::resolve(target_jh));
twisti@3131 3099 {
twisti@3131 3100 // Walk all nmethods depending on this call site.
twisti@3131 3101 MutexLocker mu(Compile_lock, thread);
twisti@3131 3102 Universe::flush_dependents_on(call_site, target);
twisti@3131 3103 }
twisti@3239 3104 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
twisti@3131 3105 }
twisti@3131 3106 JVM_END
twisti@3131 3107
jrose@2941 3108 methodOop MethodHandles::resolve_raise_exception_method(TRAPS) {
jrose@2941 3109 if (_raise_exception_method != NULL) {
jrose@2941 3110 // no need to do it twice
jrose@2941 3111 return raise_exception_method();
jrose@2941 3112 }
jrose@2941 3113 // LinkResolver::resolve_invokedynamic can reach this point
jrose@2941 3114 // because an invokedynamic has failed very early (7049415)
jrose@2941 3115 KlassHandle MHN_klass = SystemDictionaryHandles::MethodHandleNatives_klass();
jrose@2941 3116 if (MHN_klass.not_null()) {
jrose@2941 3117 TempNewSymbol raiseException_name = SymbolTable::new_symbol("raiseException", CHECK_NULL);
jrose@2941 3118 TempNewSymbol raiseException_sig = SymbolTable::new_symbol("(ILjava/lang/Object;Ljava/lang/Object;)V", CHECK_NULL);
jrose@2941 3119 methodOop raiseException_method = instanceKlass::cast(MHN_klass->as_klassOop())
jrose@2941 3120 ->find_method(raiseException_name, raiseException_sig);
jrose@2941 3121 if (raiseException_method != NULL && raiseException_method->is_static()) {
jrose@2941 3122 return raiseException_method;
jrose@2941 3123 }
jrose@2941 3124 }
jrose@2941 3125 // not found; let the caller deal with it
jrose@2941 3126 return NULL;
jrose@2941 3127 }
jrose@2941 3128 void MethodHandles::raise_exception(int code, oop actual, oop required, TRAPS) {
jrose@2941 3129 methodOop raiseException_method = resolve_raise_exception_method(CHECK);
jrose@2941 3130 if (raiseException_method != NULL &&
jrose@2941 3131 instanceKlass::cast(raiseException_method->method_holder())->is_not_initialized()) {
jrose@2941 3132 instanceKlass::cast(raiseException_method->method_holder())->initialize(CHECK);
jrose@2941 3133 // it had better be resolved by now, or maybe JSR 292 failed to load
jrose@2941 3134 raiseException_method = raise_exception_method();
jrose@2941 3135 }
jrose@2941 3136 if (raiseException_method == NULL) {
jrose@2941 3137 THROW_MSG(vmSymbols::java_lang_InternalError(), "no raiseException method");
jrose@2941 3138 }
jrose@2941 3139 JavaCallArguments args;
jrose@2941 3140 args.push_int(code);
jrose@2941 3141 args.push_oop(actual);
jrose@2941 3142 args.push_oop(required);
jrose@2941 3143 JavaValue result(T_VOID);
jrose@2941 3144 JavaCalls::call(&result, raiseException_method, &args, CHECK);
jrose@2941 3145 }
jrose@2941 3146
jrose@2919 3147 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) {
jrose@2919 3148 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL);
jrose@2919 3149 THROW_MSG_NULL(UOE_name, "MethodHandle.invoke cannot be invoked reflectively");
jrose@2919 3150 return NULL;
jrose@2919 3151 }
jrose@2919 3152 JVM_END
jrose@2919 3153
jrose@2919 3154 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv *env, jobject igmh, jobjectArray igargs)) {
jrose@2919 3155 TempNewSymbol UOE_name = SymbolTable::new_symbol("java/lang/UnsupportedOperationException", CHECK_NULL);
jrose@2919 3156 THROW_MSG_NULL(UOE_name, "MethodHandle.invokeExact cannot be invoked reflectively");
jrose@2919 3157 return NULL;
jrose@2919 3158 }
jrose@2919 3159 JVM_END
jrose@2919 3160
jrose@1161 3161
jrose@1145 3162 /// JVM_RegisterMethodHandleMethods
jrose@1145 3163
twisti@3131 3164 #undef CS // Solaris builds complain
twisti@3131 3165
jrose@1145 3166 #define LANG "Ljava/lang/"
jrose@2742 3167 #define JLINV "Ljava/lang/invoke/"
jrose@1145 3168
jrose@1145 3169 #define OBJ LANG"Object;"
jrose@1145 3170 #define CLS LANG"Class;"
jrose@1145 3171 #define STRG LANG"String;"
twisti@3131 3172 #define CS JLINV"CallSite;"
jrose@2742 3173 #define MT JLINV"MethodType;"
jrose@2742 3174 #define MH JLINV"MethodHandle;"
jrose@2742 3175 #define MEM JLINV"MemberName;"
jrose@2742 3176 #define AMH JLINV"AdapterMethodHandle;"
jrose@2742 3177 #define BMH JLINV"BoundMethodHandle;"
jrose@2742 3178 #define DMH JLINV"DirectMethodHandle;"
jrose@1145 3179
jrose@1145 3180 #define CC (char*) /*cast a literal from (const char*)*/
jrose@1145 3181 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
jrose@1145 3182
twisti@3131 3183 // These are the native methods on java.lang.invoke.MethodHandleNatives.
jrose@1145 3184 static JNINativeMethod methods[] = {
jrose@1145 3185 // void init(MemberName self, AccessibleObject ref)
twisti@3131 3186 {CC"init", CC"("AMH""MH"I)V", FN_PTR(MHN_init_AMH)},
twisti@3131 3187 {CC"init", CC"("BMH""OBJ"I)V", FN_PTR(MHN_init_BMH)},
twisti@3131 3188 {CC"init", CC"("DMH""OBJ"Z"CLS")V", FN_PTR(MHN_init_DMH)},
twisti@3131 3189 {CC"init", CC"("MT")V", FN_PTR(MHN_init_MT)},
twisti@3131 3190 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)},
twisti@3131 3191 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)},
twisti@3131 3192 {CC"resolve", CC"("MEM""CLS")V", FN_PTR(MHN_resolve_Mem)},
twisti@3131 3193 {CC"getTarget", CC"("MH"I)"OBJ, FN_PTR(MHN_getTarget)},
twisti@3131 3194 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)},
jrose@1145 3195 // static native int getNamedCon(int which, Object[] name)
twisti@3131 3196 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)},
jrose@1145 3197 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
jrose@1145 3198 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
twisti@3131 3199 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}
twisti@3131 3200 };
twisti@3131 3201
twisti@3131 3202 static JNINativeMethod call_site_methods[] = {
twisti@3131 3203 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)},
twisti@3131 3204 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}
jrose@1145 3205 };
jrose@1145 3206
jrose@2919 3207 static JNINativeMethod invoke_methods[] = {
jrose@2919 3208 // void init(MemberName self, AccessibleObject ref)
twisti@3131 3209 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)},
twisti@3131 3210 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)}
jrose@2919 3211 };
jrose@2919 3212
jrose@1145 3213 // This one function is exported, used by NativeLookup.
jrose@1145 3214
jrose@1145 3215 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
jrose@1145 3216 assert(MethodHandles::spot_check_entry_names(), "entry enum is OK");
jrose@1145 3217
twisti@2698 3218 if (!EnableInvokeDynamic) {
twisti@2698 3219 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable.");
jrose@1145 3220 return; // bind nothing
jrose@1145 3221 }
jrose@1145 3222
twisti@3131 3223 assert(!MethodHandles::enabled(), "must not be enabled");
jrose@1474 3224 bool enable_MH = true;
jrose@1474 3225
jrose@2742 3226 {
jrose@1145 3227 ThreadToNativeFromVM ttnfv(thread);
jrose@1145 3228 int status = env->RegisterNatives(MHN_class, methods, sizeof(methods)/sizeof(JNINativeMethod));
jrose@2919 3229 if (!env->ExceptionOccurred()) {
jrose@2919 3230 const char* L_MH_name = (JLINV "MethodHandle");
jrose@2919 3231 const char* MH_name = L_MH_name+1;
jrose@2919 3232 jclass MH_class = env->FindClass(MH_name);
jrose@2919 3233 status = env->RegisterNatives(MH_class, invoke_methods, sizeof(invoke_methods)/sizeof(JNINativeMethod));
jrose@2919 3234 }
jrose@1145 3235 if (env->ExceptionOccurred()) {
jrose@2742 3236 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
jrose@2742 3237 enable_MH = false;
jrose@1145 3238 env->ExceptionClear();
jrose@1145 3239 }
twisti@3131 3240
twisti@3131 3241 status = env->RegisterNatives(MHN_class, call_site_methods, sizeof(call_site_methods)/sizeof(JNINativeMethod));
twisti@3131 3242 if (env->ExceptionOccurred()) {
twisti@3131 3243 // Exception is okay until 7087357
twisti@3131 3244 env->ExceptionClear();
twisti@3131 3245 }
jrose@1145 3246 }
jrose@1161 3247
jrose@1474 3248 if (enable_MH) {
jrose@2941 3249 methodOop raiseException_method = MethodHandles::resolve_raise_exception_method(CHECK);
jrose@2941 3250 if (raiseException_method != NULL) {
jrose@2941 3251 MethodHandles::set_raise_exception_method(raiseException_method);
jrose@2742 3252 } else {
jrose@2941 3253 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
twisti@2436 3254 enable_MH = false;
jrose@1474 3255 }
jrose@1474 3256 }
jrose@1474 3257
jrose@1474 3258 if (enable_MH) {
twisti@2436 3259 MethodHandles::generate_adapters();
jrose@1474 3260 MethodHandles::set_enabled(true);
jrose@1474 3261 }
jrose@1145 3262 }
jrose@1145 3263 JVM_END

mercurial