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   __ load_heap_oop(method_temp,
 204           Address(method_temp,
 205                   NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes())));
 206   __ verify_oop(method_temp);
 207   __ z_lg(method_temp,
 208           Address(method_temp,
 209                   NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes())));
 210 
 211   if (VerifyMethodHandles && !for_compiler_entry) {
 212     // Make sure recv is already on stack.
 213     NearLabel L;
 214     Address paramSize(temp2, ConstMethod::size_of_parameters_offset());
 215 
 216     __ z_lg(temp2, Address(method_temp, Method::const_offset()));
 217     __ load_sized_value(temp2, paramSize, sizeof(u2), /*is_signed*/ false);
 218     // if (temp2 != recv) stop
 219     __ z_lg(temp2, __ argument_address(temp2, temp2, 0));
 220     __ compare64_and_branch(temp2, recv, Assembler::bcondEqual, L);
 221     __ stop("receiver not on stack");
 222     __ BIND(L);
 223   }
 224 
 225   jump_from_method_handle(_masm, method_temp, temp2, Z_R0, for_compiler_entry);
 226 
 227   BLOCK_COMMENT("} jump_to_lambda_form");
 228 }
 229 
 230 // code generation
 231 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
 232                                                                 vmIntrinsics::ID iid) {
 233   const bool not_for_compiler_entry = false;  // This is the interpreter entry.
 234   assert(is_signature_polymorphic(iid), "expected invoke iid");
 235 
 236   if (iid == vmIntrinsics::_invokeGeneric || iid == vmIntrinsics::_compiledLambdaForm) {
 237     // Perhaps surprisingly, the symbolic references visible to Java
 238     // are not directly used. They are linked to Java-generated
 239     // adapters via MethodHandleNatives.linkMethod. They all allow an
 240     // appendix argument.
 241     __ should_not_reach_here();           // Empty stubs make SG sick.
 242     return NULL;
 243   }
 244 
 245   // Z_R10: sender SP (must preserve; see prepare_to_jump_from_interprted)
 246   // Z_method: method
 247   // Z_ARG1 (Gargs): incoming argument list (must preserve)
 248   Register Z_R4_param_size = Z_R4;   // size of parameters
 249   address code_start = __ pc();
 250 
 251   // Here is where control starts out:
 252   __ align(CodeEntryAlignment);
 253 
 254   address entry_point = __ pc();
 255 
 256   if (VerifyMethodHandles) {
 257     Label L;
 258     BLOCK_COMMENT("verify_intrinsic_id {");
 259 
 260     // Supplement to 8139891: _intrinsic_id exceeded 1-byte size limit.
 261     if (Method::intrinsic_id_size_in_bytes() == 1) {
 262       __ z_cli(Address(Z_method, Method::intrinsic_id_offset_in_bytes()), (int)iid);
 263     } else {
 264       assert(Method::intrinsic_id_size_in_bytes() == 2, "size error: check Method::_intrinsic_id");
 265       __ z_lh(Z_R0_scratch, Address(Z_method, Method::intrinsic_id_offset_in_bytes()));
 266       __ z_chi(Z_R0_scratch, (int)iid);
 267     }
 268     __ z_bre(L);
 269 
 270     if (iid == vmIntrinsics::_linkToVirtual || iid == vmIntrinsics::_linkToSpecial) {
 271       // Could do this for all kinds, but would explode assembly code size.
 272       trace_method_handle(_masm, "bad Method::intrinsic_id");
 273     }
 274 
 275     __ stop("bad Method::intrinsic_id");
 276     __ bind(L);
 277 
 278     BLOCK_COMMENT("} verify_intrinsic_id");
 279   }
 280 
 281   // First task: Find out how big the argument list is.
 282   Address Z_R4_first_arg_addr;
 283   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
 284 
 285   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic,
 286          "must be _invokeBasic or a linkTo intrinsic");
 287 
 288   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
 289      Address paramSize(Z_R1_scratch, ConstMethod::size_of_parameters_offset());
 290 
 291     __ z_lg(Z_R1_scratch, Address(Z_method, Method::const_offset()));
 292     __ load_sized_value(Z_R4_param_size, paramSize, sizeof(u2), /*is_signed*/ false);
 293     Z_R4_first_arg_addr = __ argument_address(Z_R4_param_size, Z_R4_param_size, 0);
 294   } else {
 295     DEBUG_ONLY(Z_R4_param_size = noreg);
 296   }
 297 
 298   Register Z_mh = noreg;
 299   if (!is_signature_polymorphic_static(iid)) {
 300     Z_mh = Z_ARG4;
 301     __ z_lg(Z_mh, Z_R4_first_arg_addr);
 302     DEBUG_ONLY(Z_R4_param_size = noreg);
 303   }
 304 
 305   // Z_R4_first_arg_addr is live!
 306 
 307   trace_method_handle_interpreter_entry(_masm, iid);
 308 
 309   if (iid == vmIntrinsics::_invokeBasic) {
 310     __ pc(); // just for the block comment
 311     generate_method_handle_dispatch(_masm, iid, Z_mh, noreg, not_for_compiler_entry);
 312   } else {
 313     // Adjust argument list by popping the trailing MemberName argument.
 314     Register Z_recv = noreg;
 315 
 316     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
 317       // Load the receiver (not the MH; the actual MemberName's receiver)
 318       // up from the interpreter stack.
 319       __ z_lg(Z_recv = Z_R5, Z_R4_first_arg_addr);
 320       DEBUG_ONLY(Z_R4_param_size = noreg);
 321     }
 322 
 323     Register Z_member = Z_method;  // MemberName ptr; incoming method ptr is dead now
 324 
 325     __ z_lg(Z_member, __ argument_address(constant(1)));
 326     __ add2reg(Z_esp, Interpreter::stackElementSize);
 327     generate_method_handle_dispatch(_masm, iid, Z_recv, Z_member, not_for_compiler_entry);
 328   }
 329 
 330   return entry_point;
 331 }
 332 
 333 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
 334                                                     vmIntrinsics::ID iid,
 335                                                     Register receiver_reg,
 336                                                     Register member_reg,
 337                                                     bool for_compiler_entry) {
 338   assert(is_signature_polymorphic(iid), "expected invoke iid");
 339 
 340   Register temp1 = for_compiler_entry ? Z_R10 : Z_R6;
 341   Register temp2 = Z_R12;
 342   Register temp3 = Z_R11;
 343   Register temp4 = Z_R13;
 344 
 345   if (for_compiler_entry) {
 346     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : Z_ARG1),
 347            "only valid assignment");
 348   }
 349   if (receiver_reg != noreg) {
 350     assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
 351   }
 352   if (member_reg != noreg) {
 353     assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
 354   }
 355   if (!for_compiler_entry) {  // Don't trash last SP.
 356     assert_different_registers(temp1, temp2, temp3, temp4, Z_R10);
 357   }
 358 
 359   if (iid == vmIntrinsics::_invokeBasic) {
 360     __ pc(); // Just for the block comment.
 361     // Indirect through MH.form.vmentry.vmtarget.
 362     jump_to_lambda_form(_masm, receiver_reg, Z_method, Z_R1, temp3, for_compiler_entry);
 363     return;
 364   }
 365 
 366   // The method is a member invoker used by direct method handles.
 367   if (VerifyMethodHandles) {
 368     // Make sure the trailing argument really is a MemberName (caller responsibility).
 369     verify_klass(_masm, member_reg,
 370                  SystemDictionary::WK_KLASS_ENUM_NAME(MemberName_klass),
 371                  temp1, temp2,
 372                  "MemberName required for invokeVirtual etc.");
 373   }
 374 
 375   Address  member_clazz(   member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
 376   Address  member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
 377   Address  member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes()));
 378   Address  vmtarget_method(Z_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes()));
 379   Register temp1_recv_klass = temp1;
 380 
 381   if (iid != vmIntrinsics::_linkToStatic) {
 382     __ verify_oop(receiver_reg);
 383     if (iid == vmIntrinsics::_linkToSpecial) {
 384       // Don't actually load the klass; just null-check the receiver.
 385       __ null_check(receiver_reg);
 386     } else {
 387       // Load receiver klass itself.
 388       __ null_check(receiver_reg, Z_R0, oopDesc::klass_offset_in_bytes());
 389       __ load_klass(temp1_recv_klass, receiver_reg);
 390       __ verify_klass_ptr(temp1_recv_klass);
 391     }
 392     BLOCK_COMMENT("check_receiver {");
 393     // The receiver for the MemberName must be in receiver_reg.
 394     // Check the receiver against the MemberName.clazz.
 395     if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
 396       // Did not load it above...
 397       __ load_klass(temp1_recv_klass, receiver_reg);
 398       __ verify_klass_ptr(temp1_recv_klass);
 399     }
 400 
 401     if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
 402       NearLabel L_ok;
 403       Register temp2_defc = temp2;
 404 
 405       __ load_heap_oop(temp2_defc, member_clazz);
 406       load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
 407       __ verify_klass_ptr(temp2_defc);
 408       __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
 409       // If we get here, the type check failed!
 410       __ stop("receiver class disagrees with MemberName.clazz");
 411       __ bind(L_ok);
 412     }
 413     BLOCK_COMMENT("} check_receiver");
 414   }
 415   if (iid == vmIntrinsics::_linkToSpecial || iid == vmIntrinsics::_linkToStatic) {
 416     DEBUG_ONLY(temp1_recv_klass = noreg);  // These guys didn't load the recv_klass.
 417   }
 418 
 419   // Live registers at this point:
 420   //   member_reg       - MemberName that was the trailing argument.
 421   //   temp1_recv_klass - Klass of stacked receiver, if needed.
 422   //   Z_R10            - Interpreter linkage if interpreted.
 423 
 424   bool method_is_live = false;
 425 
 426   switch (iid) {
 427     case vmIntrinsics::_linkToSpecial:
 428       if (VerifyMethodHandles) {
 429         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
 430       }
 431       __ load_heap_oop(Z_method, member_vmtarget);
 432       __ z_lg(Z_method, vmtarget_method);
 433       method_is_live = true;
 434       break;
 435 
 436     case vmIntrinsics::_linkToStatic:
 437       if (VerifyMethodHandles) {
 438         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
 439       }
 440       __ load_heap_oop(Z_method, member_vmtarget);
 441       __ z_lg(Z_method, vmtarget_method);
 442       method_is_live = true;
 443       break;
 444 
 445     case vmIntrinsics::_linkToVirtual: {
 446       // Same as TemplateTable::invokevirtual, minus the CP setup and profiling.
 447       if (VerifyMethodHandles) {
 448         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
 449       }
 450 
 451       // Pick out the vtable index from the MemberName, and then we can discard it.
 452       Register temp2_index = temp2;
 453       __ z_lg(temp2_index, member_vmindex);
 454 
 455       if (VerifyMethodHandles) {
 456         // if (member_vmindex < 0) stop
 457         NearLabel L_index_ok;
 458         __ compare32_and_branch(temp2_index, constant(0), Assembler::bcondNotLow, L_index_ok);
 459         __ stop("no virtual index");
 460         __ BIND(L_index_ok);
 461       }
 462 
 463       // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget
 464       // at this point. And VerifyMethodHandles has already checked clazz, if needed.
 465 
 466       // Get target method and entry point.
 467       __ lookup_virtual_method(temp1_recv_klass, temp2_index, Z_method);
 468       method_is_live = true;
 469       break;
 470     }
 471 
 472     case vmIntrinsics::_linkToInterface: {
 473       // Same as TemplateTable::invokeinterface, minus the CP setup
 474       // and profiling, with different argument motion.
 475       if (VerifyMethodHandles) {
 476         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
 477       }
 478 
 479       Register temp3_intf = temp3;
 480 
 481       __ load_heap_oop(temp3_intf, member_clazz);
 482       load_klass_from_Class(_masm, temp3_intf, temp2, temp4);
 483 
 484       Register Z_index = Z_method;
 485 
 486       __ z_lg(Z_index, member_vmindex);
 487 
 488       if (VerifyMethodHandles) {
 489         NearLabel L;
 490         // if (member_vmindex < 0) stop
 491         __ compare32_and_branch(Z_index, constant(0), Assembler::bcondNotLow, L);
 492         __ stop("invalid vtable index for MH.invokeInterface");
 493         __ bind(L);
 494       }
 495 
 496       // Given interface, index, and recv klass, dispatch to the implementation method.
 497       Label L_no_such_interface;
 498       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
 499                                  // Note: next two args must be the same:
 500                                  Z_index, Z_method, temp2, noreg,
 501                                  L_no_such_interface);
 502       jump_from_method_handle(_masm, Z_method, temp2, Z_R0, for_compiler_entry);
 503 
 504       __ bind(L_no_such_interface);
 505 
 506       // Throw exception.
 507       __ load_const_optimized(Z_R1, StubRoutines::throw_IncompatibleClassChangeError_entry());
 508       __ z_br(Z_R1);
 509       break;
 510     }
 511 
 512     default:
 513       fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid));
 514       break;
 515   }
 516 
 517   if (method_is_live) {
 518     // Live at this point: Z_method, O5_savedSP (if interpreted).
 519 
 520     // After figuring out which concrete method to call, jump into it.
 521     // Note that this works in the interpreter with no data motion.
 522     // But the compiled version will require that rcx_recv be shifted out.
 523     jump_from_method_handle(_masm, Z_method, temp1, Z_R0, for_compiler_entry);
 524   }
 525 }
 526 
 527 #ifndef PRODUCT
 528 void trace_method_handle_stub(const char* adaptername,
 529                               oopDesc* mh,
 530                               intptr_t* sender_sp,
 531                               intptr_t* args,
 532                               intptr_t* tracing_fp) {
 533   bool has_mh = (strstr(adaptername, "/static") == NULL &&
 534                  strstr(adaptername, "linkTo") == NULL);    // Static linkers don't have MH.
 535   const char* mh_reg_name = has_mh ? "Z_R4_mh" : "Z_R4";
 536   tty->print_cr("MH %s %s=" INTPTR_FORMAT " sender_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT,
 537                 adaptername, mh_reg_name,
 538                 p2i(mh), p2i(sender_sp), p2i(args));
 539 
 540   if (Verbose) {
 541     // Dumping last frame with frame::describe.
 542 
 543     JavaThread* p = JavaThread::active();
 544 
 545     ResourceMark rm;
 546     PRESERVE_EXCEPTION_MARK; // May not be needed by safer and unexpensive here.
 547     FrameValues values;
 548 
 549     // Note: We want to allow trace_method_handle from any call site.
 550     // While trace_method_handle creates a frame, it may be entered
 551     // without a valid return PC in Z_R14 (e.g. not just after a call).
 552     // Walking that frame could lead to failures due to that invalid PC.
 553     // => carefully detect that frame when doing the stack walking.
 554 
 555     // Walk up to the right frame using the "tracing_fp" argument.
 556     frame cur_frame = os::current_frame(); // Current C frame.
 557 
 558     while (cur_frame.fp() != tracing_fp) {
 559       cur_frame = os::get_sender_for_C_frame(&cur_frame);
 560     }
 561 
 562     // Safely create a frame and call frame::describe.
 563     intptr_t *dump_sp = cur_frame.sender_sp();
 564     intptr_t *dump_fp = cur_frame.link();
 565 
 566     bool walkable = has_mh; // Whether the traced frame shoud be walkable.
 567 
 568     // The sender for cur_frame is the caller of trace_method_handle.
 569     if (walkable) {
 570       // The previous definition of walkable may have to be refined
 571       // if new call sites cause the next frame constructor to start
 572       // failing. Alternatively, frame constructors could be
 573       // modified to support the current or future non walkable
 574       // frames (but this is more intrusive and is not considered as
 575       // part of this RFE, which will instead use a simpler output).
 576       frame dump_frame = frame(dump_sp);
 577       dump_frame.describe(values, 1);
 578     } else {
 579       // Robust dump for frames which cannot be constructed from sp/younger_sp
 580       // Add descriptions without building a Java frame to avoid issues.
 581       values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
 582       values.describe(-1, dump_sp, "sp");
 583     }
 584 
 585     bool has_args = has_mh; // Whether Z_esp is meaningful.
 586 
 587     // Mark args, if seems valid (may not be valid for some adapters).
 588     if (has_args) {
 589       if ((args >= dump_sp) && (args < dump_fp)) {
 590         values.describe(-1, args, "*Z_esp");
 591       }
 592     }
 593 
 594     // Note: the unextended_sp may not be correct.
 595     tty->print_cr("  stack layout:");
 596     values.print(p);
 597     if (has_mh && mh->is_oop()) {
 598       mh->print();
 599       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
 600         if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0) {
 601           java_lang_invoke_MethodHandle::form(mh)->print();
 602         }
 603       }
 604     }
 605   }
 606 }
 607 
 608 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 609   if (!TraceMethodHandles) { return; }
 610 
 611   BLOCK_COMMENT("trace_method_handle {");
 612 
 613   // Save argument registers (they are used in raise exception stub).
 614   __ z_stg(Z_ARG1, Address(Z_SP, 16));
 615   __ z_stg(Z_ARG2, Address(Z_SP, 24));
 616   __ z_stg(Z_ARG3, Address(Z_SP, 32));
 617   __ z_stg(Z_ARG4, Address(Z_SP, 40));
 618   __ z_stg(Z_ARG5, Address(Z_SP, 48));
 619 
 620   // Setup arguments.
 621   __ z_lgr(Z_ARG2, Z_ARG4); // mh, see generate_method_handle_interpreter_entry()
 622   __ z_lgr(Z_ARG3, Z_R10);  // sender_sp
 623   __ z_lgr(Z_ARG4, Z_esp);
 624   __ load_const_optimized(Z_ARG1, (void *)adaptername);
 625   __ z_lgr(Z_ARG5, Z_SP);   // tracing_fp
 626   __ save_return_pc();      // saves Z_R14
 627   __ push_frame_abi160(0);
 628   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
 629   __ pop_frame();
 630   __ restore_return_pc();   // restores to Z_R14
 631   __ z_lg(Z_ARG1, Address(Z_SP, 16));
 632   __ z_lg(Z_ARG2, Address(Z_SP, 24));
 633   __ z_lg(Z_ARG3, Address(Z_SP, 32));
 634   __ z_lg(Z_ARG4, Address(Z_SP, 40));
 635   __ z_lg(Z_ARG5, Address(Z_SP, 45));
 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