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