1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2017, SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "jvm.h"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "classfile/javaClasses.inline.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "prims/methodHandles.hpp"
  34 #include "utilities/preserveException.hpp"
  35 
  36 #ifdef PRODUCT
  37 #define __ _masm->
  38 #define BLOCK_COMMENT(str) /* nothing */
  39 #else
  40 #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm):_masm)->
  41 #define BLOCK_COMMENT(str) __ block_comment(str)
  42 #endif
  43 
  44 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  45 
  46 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
  47 static RegisterOrConstant constant(int value) {
  48   return RegisterOrConstant(value);
  49 }
  50 
  51 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg,
  52                                           Register temp_reg, Register temp2_reg) {
  53   if (VerifyMethodHandles) {
  54     verify_klass(_masm, klass_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_Class),
  55                  temp_reg, temp2_reg, "MH argument is a Class");
  56   }
  57   __ z_lg(klass_reg, Address(klass_reg, java_lang_Class::klass_offset_in_bytes()));
  58 }
  59 
  60 
  61 #ifdef ASSERT
  62 static int check_nonzero(const char* xname, int x) {
  63   assert(x != 0, "%s should be nonzero", xname);
  64   return x;
  65 }
  66 #define NONZERO(x) check_nonzero(#x, x)
  67 #else
  68 #define NONZERO(x) (x)
  69 #endif
  70 
  71 #ifdef ASSERT
  72 void MethodHandles::verify_klass(MacroAssembler* _masm,
  73                                  Register obj_reg, SystemDictionary::WKID klass_id,
  74                                  Register temp_reg, Register temp2_reg,
  75                                  const char* error_message) {
  76 
  77   InstanceKlass** klass_addr = SystemDictionary::well_known_klass_addr(klass_id);
  78   Klass* klass = SystemDictionary::well_known_klass(klass_id);
  79 
  80   assert(temp_reg != Z_R0 && // Is used as base register!
  81          temp_reg != noreg && temp2_reg != noreg, "need valid registers!");
  82 
  83   NearLabel L_ok, L_bad;
  84 
  85   BLOCK_COMMENT("verify_klass {");
  86 
  87   __ verify_oop(obj_reg);
  88   __ compareU64_and_branch(obj_reg, (intptr_t)0L, Assembler::bcondEqual, L_bad);
  89   __ load_klass(temp_reg, obj_reg);
  90   // klass_addr is a klass in allstatic SystemDictionaryHandles. Can't get GCed.
  91   __ load_const_optimized(temp2_reg, (address)klass_addr);
  92   __ z_lg(temp2_reg, Address(temp2_reg));
  93   __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok);
  94 
  95   intptr_t super_check_offset = klass->super_check_offset();
  96   __ z_lg(temp_reg, Address(temp_reg, super_check_offset));
  97   __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok);
  98   __ BIND(L_bad);
  99   __ stop(error_message);
 100   __ BIND(L_ok);
 101 
 102   BLOCK_COMMENT("} verify_klass");
 103 }
 104 
 105 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind,
 106                                     Register member_reg, Register temp  ) {
 107   NearLabel L;
 108   BLOCK_COMMENT("verify_ref_kind {");
 109 
 110   __ z_llgf(temp,
 111             Address(member_reg,
 112                     NONZERO(java_lang_invoke_MemberName::flags_offset_in_bytes())));
 113   __ z_srl(temp,  java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
 114   __ z_nilf(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
 115   __ compare32_and_branch(temp, constant(ref_kind), Assembler::bcondEqual, L);
 116 
 117   {
 118     char *buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
 119 
 120     jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind);
 121     if (ref_kind == JVM_REF_invokeVirtual || ref_kind == JVM_REF_invokeSpecial) {
 122       // Could do this for all ref_kinds, but would explode assembly code size.
 123       trace_method_handle(_masm, buf);
 124     }
 125     __ stop(buf);
 126   }
 127 
 128   BLOCK_COMMENT("} verify_ref_kind");
 129 
 130   __ bind(L);
 131 }
 132 #endif // ASSERT
 133 
 134 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target,
 135                                             Register temp, bool for_compiler_entry) {
 136   assert(method == Z_method, "interpreter calling convention");
 137   __ verify_method_ptr(method);
 138 
 139   assert(target != method, "don 't you kill the method reg!");
 140 
 141   Label L_no_such_method;
 142 
 143   if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
 144     // JVMTI events, such as single-stepping, are implemented partly
 145     // by avoiding running compiled code in threads for which the
 146     // event is enabled. Check here for interp_only_mode if these
 147     // events CAN be enabled.
 148     __ verify_thread();
 149 
 150     Label run_compiled_code;
 151 
 152     __ load_and_test_int(temp, Address(Z_thread, JavaThread::interp_only_mode_offset()));
 153     __ z_bre(run_compiled_code);
 154 
 155     // Null method test is replicated below in compiled case,
 156     // it might be able to address across the verify_thread().
 157     __ z_ltgr(temp, method);
 158     __ z_bre(L_no_such_method);
 159 
 160     __ z_lg(target, Address(method, Method::interpreter_entry_offset()));
 161     __ z_br(target);
 162 
 163     __ bind(run_compiled_code);
 164   }
 165 
 166   // Compiled case, either static or fall-through from runtime conditional.
 167   __ z_ltgr(temp, method);
 168   __ z_bre(L_no_such_method);
 169 
 170   ByteSize offset = for_compiler_entry ?
 171                        Method::from_compiled_offset() : Method::from_interpreted_offset();
 172   Address method_from(method, offset);
 173 
 174   __ z_lg(target, method_from);
 175   __ z_br(target);
 176 
 177   __ bind(L_no_such_method);
 178   assert(StubRoutines::throw_AbstractMethodError_entry() != NULL, "not yet generated!");
 179   __ load_const_optimized(target, StubRoutines::throw_AbstractMethodError_entry());
 180   __ z_br(target);
 181 }
 182 
 183 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
 184                                         Register recv, Register method_temp,
 185                                         Register temp2, Register temp3,
 186                                         bool for_compiler_entry) {
 187 
 188   // This is the initial entry point of a lazy method handle.
 189   // After type checking, it picks up the invoker from the LambdaForm.
 190   assert_different_registers(recv, method_temp, temp2, temp3);
 191   assert(method_temp == Z_method, "required register for loading method");
 192 
 193   BLOCK_COMMENT("jump_to_lambda_form {");
 194 
 195   // Load the invoker, as MH -> MH.form -> LF.vmentry
 196   __ verify_oop(recv);
 197   __ load_heap_oop(method_temp,
 198                      Address(recv,
 199                              NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes())));
 200   __ verify_oop(method_temp);
 201   __ load_heap_oop(method_temp,
 202                      Address(method_temp,
 203                              NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes())));
 204   __ verify_oop(method_temp);
 205   __ load_heap_oop(method_temp,
 206           Address(method_temp,
 207                   NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes())));
 208   __ verify_oop(method_temp);
 209   __ z_lg(method_temp,
 210           Address(method_temp,
 211                   NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes())));
 212 
 213   if (VerifyMethodHandles && !for_compiler_entry) {
 214     // Make sure recv is already on stack.
 215     NearLabel L;
 216     Address paramSize(temp2, ConstMethod::size_of_parameters_offset());
 217 
 218     __ z_lg(temp2, Address(method_temp, Method::const_offset()));
 219     __ load_sized_value(temp2, paramSize, sizeof(u2), /*is_signed*/ false);
 220     // if (temp2 != recv) stop
 221     __ z_lg(temp2, __ argument_address(temp2, temp2, 0));
 222     __ compare64_and_branch(temp2, recv, Assembler::bcondEqual, L);
 223     __ stop("receiver not on stack");
 224     __ BIND(L);
 225   }
 226 
 227   jump_from_method_handle(_masm, method_temp, temp2, Z_R0, for_compiler_entry);
 228 
 229   BLOCK_COMMENT("} jump_to_lambda_form");
 230 }
 231 
 232 // code generation
 233 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
 234                                                                 vmIntrinsics::ID iid) {
 235   const bool not_for_compiler_entry = false;  // This is the interpreter entry.
 236   assert(is_signature_polymorphic(iid), "expected invoke iid");
 237 
 238   if (iid == vmIntrinsics::_invokeGeneric || iid == vmIntrinsics::_compiledLambdaForm) {
 239     // Perhaps surprisingly, the symbolic references visible to Java
 240     // are not directly used. They are linked to Java-generated
 241     // adapters via MethodHandleNatives.linkMethod. They all allow an
 242     // appendix argument.
 243     __ should_not_reach_here();           // Empty stubs make SG sick.
 244     return NULL;
 245   }
 246 
 247   // Z_R10: sender SP (must preserve; see prepare_to_jump_from_interprted)
 248   // Z_method: method
 249   // Z_ARG1 (Gargs): incoming argument list (must preserve)
 250   Register Z_R4_param_size = Z_R4;   // size of parameters
 251   address code_start = __ pc();
 252 
 253   // Here is where control starts out:
 254   __ align(CodeEntryAlignment);
 255 
 256   address entry_point = __ pc();
 257 
 258   if (VerifyMethodHandles) {
 259     Label L;
 260     BLOCK_COMMENT("verify_intrinsic_id {");
 261 
 262     // Supplement to 8139891: _intrinsic_id exceeded 1-byte size limit.
 263     if (Method::intrinsic_id_size_in_bytes() == 1) {
 264       __ z_cli(Address(Z_method, Method::intrinsic_id_offset_in_bytes()), (int)iid);
 265     } else {
 266       assert(Method::intrinsic_id_size_in_bytes() == 2, "size error: check Method::_intrinsic_id");
 267       __ z_lh(Z_R0_scratch, Address(Z_method, Method::intrinsic_id_offset_in_bytes()));
 268       __ z_chi(Z_R0_scratch, (int)iid);
 269     }
 270     __ z_bre(L);
 271 
 272     if (iid == vmIntrinsics::_linkToVirtual || iid == vmIntrinsics::_linkToSpecial) {
 273       // Could do this for all kinds, but would explode assembly code size.
 274       trace_method_handle(_masm, "bad Method::intrinsic_id");
 275     }
 276 
 277     __ stop("bad Method::intrinsic_id");
 278     __ bind(L);
 279 
 280     BLOCK_COMMENT("} verify_intrinsic_id");
 281   }
 282 
 283   // First task: Find out how big the argument list is.
 284   Address Z_R4_first_arg_addr;
 285   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
 286 
 287   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic,
 288          "must be _invokeBasic or a linkTo intrinsic");
 289 
 290   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
 291      Address paramSize(Z_R1_scratch, ConstMethod::size_of_parameters_offset());
 292 
 293     __ z_lg(Z_R1_scratch, Address(Z_method, Method::const_offset()));
 294     __ load_sized_value(Z_R4_param_size, paramSize, sizeof(u2), /*is_signed*/ false);
 295     Z_R4_first_arg_addr = __ argument_address(Z_R4_param_size, Z_R4_param_size, 0);
 296   } else {
 297     DEBUG_ONLY(Z_R4_param_size = noreg);
 298   }
 299 
 300   Register Z_mh = noreg;
 301   if (!is_signature_polymorphic_static(iid)) {
 302     Z_mh = Z_ARG4;
 303     __ z_lg(Z_mh, Z_R4_first_arg_addr);
 304     DEBUG_ONLY(Z_R4_param_size = noreg);
 305   }
 306 
 307   // Z_R4_first_arg_addr is live!
 308 
 309   trace_method_handle_interpreter_entry(_masm, iid);
 310 
 311   if (iid == vmIntrinsics::_invokeBasic) {
 312     __ pc(); // just for the block comment
 313     generate_method_handle_dispatch(_masm, iid, Z_mh, noreg, not_for_compiler_entry);
 314   } else {
 315     // Adjust argument list by popping the trailing MemberName argument.
 316     Register Z_recv = noreg;
 317 
 318     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
 319       // Load the receiver (not the MH; the actual MemberName's receiver)
 320       // up from the interpreter stack.
 321       __ z_lg(Z_recv = Z_R5, Z_R4_first_arg_addr);
 322       DEBUG_ONLY(Z_R4_param_size = noreg);
 323     }
 324 
 325     Register Z_member = Z_method;  // MemberName ptr; incoming method ptr is dead now
 326 
 327     __ z_lg(Z_member, __ argument_address(constant(1)));
 328     __ add2reg(Z_esp, Interpreter::stackElementSize);
 329     generate_method_handle_dispatch(_masm, iid, Z_recv, Z_member, not_for_compiler_entry);
 330   }
 331 
 332   return entry_point;
 333 }
 334 
 335 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
 336                                                     vmIntrinsics::ID iid,
 337                                                     Register receiver_reg,
 338                                                     Register member_reg,
 339                                                     bool for_compiler_entry) {
 340   assert(is_signature_polymorphic(iid), "expected invoke iid");
 341 
 342   Register temp1 = for_compiler_entry ? Z_R10 : Z_R6;
 343   Register temp2 = Z_R12;
 344   Register temp3 = Z_R11;
 345   Register temp4 = Z_R13;
 346 
 347   if (for_compiler_entry) {
 348     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : Z_ARG1),
 349            "only valid assignment");
 350   }
 351   if (receiver_reg != noreg) {
 352     assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
 353   }
 354   if (member_reg != noreg) {
 355     assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
 356   }
 357   if (!for_compiler_entry) {  // Don't trash last SP.
 358     assert_different_registers(temp1, temp2, temp3, temp4, Z_R10);
 359   }
 360 
 361   if (iid == vmIntrinsics::_invokeBasic) {
 362     __ pc(); // Just for the block comment.
 363     // Indirect through MH.form.vmentry.vmtarget.
 364     jump_to_lambda_form(_masm, receiver_reg, Z_method, Z_R1, temp3, for_compiler_entry);
 365     return;
 366   }
 367 
 368   // The method is a member invoker used by direct method handles.
 369   if (VerifyMethodHandles) {
 370     // Make sure the trailing argument really is a MemberName (caller responsibility).
 371     verify_klass(_masm, member_reg,
 372                  SystemDictionary::WK_KLASS_ENUM_NAME(MemberName_klass),
 373                  temp1, temp2,
 374                  "MemberName required for invokeVirtual etc.");
 375   }
 376 
 377   Address  member_clazz(   member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
 378   Address  member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
 379   Address  member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes()));
 380   Address  vmtarget_method(Z_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes()));
 381   Register temp1_recv_klass = temp1;
 382 
 383   if (iid != vmIntrinsics::_linkToStatic) {
 384     __ verify_oop(receiver_reg);
 385     if (iid == vmIntrinsics::_linkToSpecial) {
 386       // Don't actually load the klass; just null-check the receiver.
 387       __ null_check(receiver_reg);
 388     } else {
 389       // Load receiver klass itself.
 390       __ null_check(receiver_reg, Z_R0, oopDesc::klass_offset_in_bytes());
 391       __ load_klass(temp1_recv_klass, receiver_reg);
 392       __ verify_klass_ptr(temp1_recv_klass);
 393     }
 394     BLOCK_COMMENT("check_receiver {");
 395     // The receiver for the MemberName must be in receiver_reg.
 396     // Check the receiver against the MemberName.clazz.
 397     if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
 398       // Did not load it above...
 399       __ load_klass(temp1_recv_klass, receiver_reg);
 400       __ verify_klass_ptr(temp1_recv_klass);
 401     }
 402 
 403     if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
 404       NearLabel L_ok;
 405       Register temp2_defc = temp2;
 406 
 407       __ load_heap_oop(temp2_defc, member_clazz);
 408       load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
 409       __ verify_klass_ptr(temp2_defc);
 410       __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
 411       // If we get here, the type check failed!
 412       __ stop("receiver class disagrees with MemberName.clazz");
 413       __ bind(L_ok);
 414     }
 415     BLOCK_COMMENT("} check_receiver");
 416   }
 417   if (iid == vmIntrinsics::_linkToSpecial || iid == vmIntrinsics::_linkToStatic) {
 418     DEBUG_ONLY(temp1_recv_klass = noreg);  // These guys didn't load the recv_klass.
 419   }
 420 
 421   // Live registers at this point:
 422   //   member_reg       - MemberName that was the trailing argument.
 423   //   temp1_recv_klass - Klass of stacked receiver, if needed.
 424   //   Z_R10            - Interpreter linkage if interpreted.
 425 
 426   bool method_is_live = false;
 427 
 428   switch (iid) {
 429     case vmIntrinsics::_linkToSpecial:
 430       if (VerifyMethodHandles) {
 431         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
 432       }
 433       __ load_heap_oop(Z_method, member_vmtarget);
 434       __ z_lg(Z_method, vmtarget_method);
 435       method_is_live = true;
 436       break;
 437 
 438     case vmIntrinsics::_linkToStatic:
 439       if (VerifyMethodHandles) {
 440         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
 441       }
 442       __ load_heap_oop(Z_method, member_vmtarget);
 443       __ z_lg(Z_method, vmtarget_method);
 444       method_is_live = true;
 445       break;
 446 
 447     case vmIntrinsics::_linkToVirtual: {
 448       // Same as TemplateTable::invokevirtual, minus the CP setup and profiling.
 449       if (VerifyMethodHandles) {
 450         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
 451       }
 452 
 453       // Pick out the vtable index from the MemberName, and then we can discard it.
 454       Register temp2_index = temp2;
 455       __ z_lg(temp2_index, member_vmindex);
 456 
 457       if (VerifyMethodHandles) {
 458         // if (member_vmindex < 0) stop
 459         NearLabel L_index_ok;
 460         __ compare32_and_branch(temp2_index, constant(0), Assembler::bcondNotLow, L_index_ok);
 461         __ stop("no virtual index");
 462         __ BIND(L_index_ok);
 463       }
 464 
 465       // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget
 466       // at this point. And VerifyMethodHandles has already checked clazz, if needed.
 467 
 468       // Get target method and entry point.
 469       __ lookup_virtual_method(temp1_recv_klass, temp2_index, Z_method);
 470       method_is_live = true;
 471       break;
 472     }
 473 
 474     case vmIntrinsics::_linkToInterface: {
 475       // Same as TemplateTable::invokeinterface, minus the CP setup
 476       // and profiling, with different argument motion.
 477       if (VerifyMethodHandles) {
 478         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
 479       }
 480 
 481       Register temp3_intf = temp3;
 482 
 483       __ load_heap_oop(temp3_intf, member_clazz);
 484       load_klass_from_Class(_masm, temp3_intf, temp2, temp4);
 485 
 486       Register Z_index = Z_method;
 487 
 488       __ z_lg(Z_index, member_vmindex);
 489 
 490       if (VerifyMethodHandles) {
 491         NearLabel L;
 492         // if (member_vmindex < 0) stop
 493         __ compare32_and_branch(Z_index, constant(0), Assembler::bcondNotLow, L);
 494         __ stop("invalid vtable index for MH.invokeInterface");
 495         __ bind(L);
 496       }
 497 
 498       // Given interface, index, and recv klass, dispatch to the implementation method.
 499       Label L_no_such_interface;
 500       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
 501                                  // Note: next two args must be the same:
 502                                  Z_index, Z_method, temp2,
 503                                  L_no_such_interface);
 504       jump_from_method_handle(_masm, Z_method, temp2, Z_R0, for_compiler_entry);
 505 
 506       __ bind(L_no_such_interface);
 507 
 508       // Throw exception.
 509       __ load_const_optimized(Z_R1, StubRoutines::throw_IncompatibleClassChangeError_entry());
 510       __ z_br(Z_R1);
 511       break;
 512     }
 513 
 514     default:
 515       fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid));
 516       break;
 517   }
 518 
 519   if (method_is_live) {
 520     // Live at this point: Z_method, O5_savedSP (if interpreted).
 521 
 522     // After figuring out which concrete method to call, jump into it.
 523     // Note that this works in the interpreter with no data motion.
 524     // But the compiled version will require that rcx_recv be shifted out.
 525     jump_from_method_handle(_masm, Z_method, temp1, Z_R0, for_compiler_entry);
 526   }
 527 }
 528 
 529 #ifndef PRODUCT
 530 void trace_method_handle_stub(const char* adaptername,
 531                               oopDesc* mh,
 532                               intptr_t* sender_sp,
 533                               intptr_t* args,
 534                               intptr_t* tracing_fp) {
 535   bool has_mh = (strstr(adaptername, "/static") == NULL &&
 536                  strstr(adaptername, "linkTo") == NULL);    // Static linkers don't have MH.
 537   const char* mh_reg_name = has_mh ? "Z_R4_mh" : "Z_R4";
 538   tty->print_cr("MH %s %s=" INTPTR_FORMAT " sender_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT,
 539                 adaptername, mh_reg_name,
 540                 p2i(mh), p2i(sender_sp), p2i(args));
 541 
 542   if (Verbose) {
 543     // Dumping last frame with frame::describe.
 544 
 545     JavaThread* p = JavaThread::active();
 546 
 547     ResourceMark rm;
 548     PRESERVE_EXCEPTION_MARK; // May not be needed by safer and unexpensive here.
 549     FrameValues values;
 550 
 551     // Note: We want to allow trace_method_handle from any call site.
 552     // While trace_method_handle creates a frame, it may be entered
 553     // without a valid return PC in Z_R14 (e.g. not just after a call).
 554     // Walking that frame could lead to failures due to that invalid PC.
 555     // => carefully detect that frame when doing the stack walking.
 556 
 557     // Walk up to the right frame using the "tracing_fp" argument.
 558     frame cur_frame = os::current_frame(); // Current C frame.
 559 
 560     while (cur_frame.fp() != tracing_fp) {
 561       cur_frame = os::get_sender_for_C_frame(&cur_frame);
 562     }
 563 
 564     // Safely create a frame and call frame::describe.
 565     intptr_t *dump_sp = cur_frame.sender_sp();
 566     intptr_t *dump_fp = cur_frame.link();
 567 
 568     bool walkable = has_mh; // Whether the traced frame shoud be walkable.
 569 
 570     // The sender for cur_frame is the caller of trace_method_handle.
 571     if (walkable) {
 572       // The previous definition of walkable may have to be refined
 573       // if new call sites cause the next frame constructor to start
 574       // failing. Alternatively, frame constructors could be
 575       // modified to support the current or future non walkable
 576       // frames (but this is more intrusive and is not considered as
 577       // part of this RFE, which will instead use a simpler output).
 578       frame dump_frame = frame(dump_sp);
 579       dump_frame.describe(values, 1);
 580     } else {
 581       // Robust dump for frames which cannot be constructed from sp/younger_sp
 582       // Add descriptions without building a Java frame to avoid issues.
 583       values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
 584       values.describe(-1, dump_sp, "sp");
 585     }
 586 
 587     bool has_args = has_mh; // Whether Z_esp is meaningful.
 588 
 589     // Mark args, if seems valid (may not be valid for some adapters).
 590     if (has_args) {
 591       if ((args >= dump_sp) && (args < dump_fp)) {
 592         values.describe(-1, args, "*Z_esp");
 593       }
 594     }
 595 
 596     // Note: the unextended_sp may not be correct.
 597     tty->print_cr("  stack layout:");
 598     values.print(p);
 599     if (has_mh && oopDesc::is_oop(mh)) {
 600       mh->print();
 601       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
 602         if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0) {
 603           java_lang_invoke_MethodHandle::form(mh)->print();
 604         }
 605       }
 606     }
 607   }
 608 }
 609 
 610 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 611   if (!TraceMethodHandles) { return; }
 612 
 613   // If arg registers are contiguous, we can use STMG/LMG.
 614   assert((Z_ARG5->encoding() - Z_ARG1->encoding() + 1) == RegisterImpl::number_of_arg_registers, "Oops");
 615 
 616   BLOCK_COMMENT("trace_method_handle {");
 617 
 618   // Save argument registers (they are used in raise exception stub).
 619   // Argument registers have contiguous register numbers -> we can use stmg/lmg.
 620   __ z_stmg(Z_ARG1, Z_ARG5, 16, Z_SP);
 621 
 622   // Setup arguments.
 623   __ z_lgr(Z_ARG2, Z_ARG4); // mh, see generate_method_handle_interpreter_entry()
 624   __ z_lgr(Z_ARG3, Z_R10);  // sender_sp
 625   __ z_lgr(Z_ARG4, Z_esp);
 626   __ load_const_optimized(Z_ARG1, (void *)adaptername);
 627   __ z_lgr(Z_ARG5, Z_SP);   // tracing_fp
 628   __ save_return_pc();      // saves Z_R14
 629   __ push_frame_abi160(0);
 630   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
 631   __ pop_frame();
 632   __ restore_return_pc();   // restores to Z_R14
 633 
 634   // Restore argument registers
 635   __ z_lmg(Z_ARG1, Z_ARG5, 16, Z_SP);
 636   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, 50, -1);
 637   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, -1, 5);
 638 
 639   BLOCK_COMMENT("} trace_method_handle");
 640 }
 641 #endif // !PRODUCT