1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 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 "runtime/frame.inline.hpp"
  35 #include "utilities/preserveException.hpp"
  36 
  37 #define __ _masm->
  38 
  39 #ifdef PRODUCT
  40 #define BLOCK_COMMENT(str) // nothing
  41 #else
  42 #define BLOCK_COMMENT(str) __ block_comment(str)
  43 #endif
  44 
  45 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  46 
  47 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
  48 inline static RegisterOrConstant constant(int value) {
  49   return RegisterOrConstant(value);
  50 }
  51 
  52 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg,
  53                                           Register temp_reg, Register temp2_reg) {
  54   if (VerifyMethodHandles) {
  55     verify_klass(_masm, klass_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_Class),
  56                  temp_reg, temp2_reg, "MH argument is a Class");
  57   }
  58   __ ld(klass_reg, java_lang_Class::klass_offset_in_bytes(), klass_reg);
  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 //ASSERT
  68 #define NONZERO(x) (x)
  69 #endif //ASSERT
  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   InstanceKlass** klass_addr = SystemDictionary::well_known_klass_addr(klass_id);
  77   Klass* klass = SystemDictionary::well_known_klass(klass_id);
  78   Label L_ok, L_bad;
  79   BLOCK_COMMENT("verify_klass {");
  80   __ verify_oop(obj_reg);
  81   __ cmpdi(CCR0, obj_reg, 0);
  82   __ beq(CCR0, L_bad);
  83   __ load_klass(temp_reg, obj_reg);
  84   __ load_const_optimized(temp2_reg, (address) klass_addr);
  85   __ ld(temp2_reg, 0, temp2_reg);
  86   __ cmpd(CCR0, temp_reg, temp2_reg);
  87   __ beq(CCR0, L_ok);
  88   __ ld(temp_reg, klass->super_check_offset(), temp_reg);
  89   __ cmpd(CCR0, temp_reg, temp2_reg);
  90   __ beq(CCR0, L_ok);
  91   __ BIND(L_bad);
  92   __ stop(error_message);
  93   __ BIND(L_ok);
  94   BLOCK_COMMENT("} verify_klass");
  95 }
  96 
  97 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {
  98   Label L;
  99   BLOCK_COMMENT("verify_ref_kind {");
 100   __ load_sized_value(temp, NONZERO(java_lang_invoke_MemberName::flags_offset_in_bytes()), member_reg,
 101                       sizeof(u4), /*is_signed*/ false);
 102   // assert(sizeof(u4) == sizeof(java.lang.invoke.MemberName.flags), "");
 103   __ srwi( temp, temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
 104   __ andi(temp, temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
 105   __ cmpwi(CCR1, temp, ref_kind);
 106   __ beq(CCR1, L);
 107   { char* buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
 108     jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind);
 109     if (ref_kind == JVM_REF_invokeVirtual ||
 110         ref_kind == JVM_REF_invokeSpecial)
 111       // could do this for all ref_kinds, but would explode assembly code size
 112       trace_method_handle(_masm, buf);
 113     __ stop(buf);
 114   }
 115   BLOCK_COMMENT("} verify_ref_kind");
 116   __ BIND(L);
 117 }
 118 
 119 #endif // ASSERT
 120 
 121 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp,
 122                                             bool for_compiler_entry) {
 123   Label L_no_such_method;
 124   assert(method == R19_method, "interpreter calling convention");
 125   assert_different_registers(method, target, temp);
 126 
 127   if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
 128     Label run_compiled_code;
 129     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 130     // compiled code in threads for which the event is enabled.  Check here for
 131     // interp_only_mode if these events CAN be enabled.
 132     __ verify_thread();
 133     __ lwz(temp, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
 134     __ cmplwi(CCR0, temp, 0);
 135     __ beq(CCR0, run_compiled_code);
 136     // Null method test is replicated below in compiled case,
 137     // it might be able to address across the verify_thread()
 138     __ cmplwi(CCR0, R19_method, 0);
 139     __ beq(CCR0, L_no_such_method);
 140     __ ld(target, in_bytes(Method::interpreter_entry_offset()), R19_method);
 141     __ mtctr(target);
 142     __ bctr();
 143     __ BIND(run_compiled_code);
 144   }
 145 
 146   // Compiled case, either static or fall-through from runtime conditional
 147   __ cmplwi(CCR0, R19_method, 0);
 148   __ beq(CCR0, L_no_such_method);
 149 
 150   const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
 151                                                      Method::from_interpreted_offset();
 152   __ ld(target, in_bytes(entry_offset), R19_method);
 153   __ mtctr(target);
 154   __ bctr();
 155 
 156   __ bind(L_no_such_method);
 157   assert(StubRoutines::throw_AbstractMethodError_entry() != NULL, "not yet generated!");
 158   __ load_const_optimized(target, StubRoutines::throw_AbstractMethodError_entry());
 159   __ mtctr(target);
 160   __ bctr();
 161 }
 162 
 163 
 164 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
 165                                         Register recv, Register method_temp,
 166                                         Register temp2, Register temp3,
 167                                         bool for_compiler_entry) {
 168   BLOCK_COMMENT("jump_to_lambda_form {");
 169   // This is the initial entry point of a lazy method handle.
 170   // After type checking, it picks up the invoker from the LambdaForm.
 171   assert_different_registers(recv, method_temp, temp2);  // temp3 is only passed on
 172   assert(method_temp == R19_method, "required register for loading method");
 173 
 174   // Load the invoker, as MH -> MH.form -> LF.vmentry
 175   __ verify_oop(recv);
 176   __ load_heap_oop_not_null(method_temp, NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes()), recv, temp2);
 177   __ verify_oop(method_temp);
 178   __ load_heap_oop_not_null(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes()), method_temp, temp2);
 179   __ verify_oop(method_temp);
 180   __ load_heap_oop_not_null(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes()), method_temp);
 181   __ verify_oop(method_temp);
 182   __ ld(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes()), method_temp);
 183 
 184   if (VerifyMethodHandles && !for_compiler_entry) {
 185     // Make sure recv is already on stack.
 186     __ ld(temp2, in_bytes(Method::const_offset()), method_temp);
 187     __ load_sized_value(temp2, in_bytes(ConstMethod::size_of_parameters_offset()), temp2,
 188                         sizeof(u2), /*is_signed*/ false);
 189     // assert(sizeof(u2) == sizeof(ConstMethod::_size_of_parameters), "");
 190     Label L;
 191     __ ld(temp2, __ argument_offset(temp2, temp2, 0), R15_esp);
 192     __ cmpd(CCR1, temp2, recv);
 193     __ beq(CCR1, L);
 194     __ stop("receiver not on stack");
 195     __ BIND(L);
 196   }
 197 
 198   jump_from_method_handle(_masm, method_temp, temp2, temp3, for_compiler_entry);
 199   BLOCK_COMMENT("} jump_to_lambda_form");
 200 }
 201 
 202 
 203 
 204 // Code generation
 205 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
 206                                                                 vmIntrinsics::ID iid) {
 207   const bool not_for_compiler_entry = false;  // this is the interpreter entry
 208   assert(is_signature_polymorphic(iid), "expected invoke iid");
 209   if (iid == vmIntrinsics::_invokeGeneric ||
 210       iid == vmIntrinsics::_compiledLambdaForm) {
 211     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
 212     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
 213     // They all allow an appendix argument.
 214     __ stop("Should not reach here");           // empty stubs make SG sick
 215     return NULL;
 216   }
 217 
 218   Register argbase    = R15_esp; // parameter (preserved)
 219   Register argslot    = R3;
 220   Register temp1      = R6;
 221   Register param_size = R7;
 222 
 223   // here's where control starts out:
 224   __ align(CodeEntryAlignment);
 225   address entry_point = __ pc();
 226 
 227   if (VerifyMethodHandles) {
 228     assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
 229 
 230     Label L;
 231     BLOCK_COMMENT("verify_intrinsic_id {");
 232     __ load_sized_value(temp1, Method::intrinsic_id_offset_in_bytes(), R19_method,
 233                         sizeof(u2), /*is_signed*/ false);
 234     __ cmpwi(CCR1, temp1, (int) iid);
 235     __ beq(CCR1, L);
 236     if (iid == vmIntrinsics::_linkToVirtual ||
 237         iid == vmIntrinsics::_linkToSpecial) {
 238       // could do this for all kinds, but would explode assembly code size
 239       trace_method_handle(_masm, "bad Method*:intrinsic_id");
 240     }
 241     __ stop("bad Method*::intrinsic_id");
 242     __ BIND(L);
 243     BLOCK_COMMENT("} verify_intrinsic_id");
 244   }
 245 
 246   // First task:  Find out how big the argument list is.
 247   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
 248   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
 249   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
 250     __ ld(param_size, in_bytes(Method::const_offset()), R19_method);
 251     __ load_sized_value(param_size, in_bytes(ConstMethod::size_of_parameters_offset()), param_size,
 252                         sizeof(u2), /*is_signed*/ false);
 253     // assert(sizeof(u2) == sizeof(ConstMethod::_size_of_parameters), "");
 254   } else {
 255     DEBUG_ONLY(param_size = noreg);
 256   }
 257 
 258   Register tmp_mh = noreg;
 259   if (!is_signature_polymorphic_static(iid)) {
 260     __ ld(tmp_mh = temp1, __ argument_offset(param_size, param_size, 0), argbase);
 261     DEBUG_ONLY(param_size = noreg);
 262   }
 263 
 264   if (TraceMethodHandles) {
 265     if (tmp_mh != noreg) {
 266       __ mr(R23_method_handle, tmp_mh);  // make stub happy
 267     }
 268     trace_method_handle_interpreter_entry(_masm, iid);
 269   }
 270 
 271   if (iid == vmIntrinsics::_invokeBasic) {
 272     generate_method_handle_dispatch(_masm, iid, tmp_mh, noreg, not_for_compiler_entry);
 273 
 274   } else {
 275     // Adjust argument list by popping the trailing MemberName argument.
 276     Register tmp_recv = noreg;
 277     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
 278       // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
 279       __ ld(tmp_recv = temp1, __ argument_offset(param_size, param_size, 0), argbase);
 280       DEBUG_ONLY(param_size = noreg);
 281     }
 282     Register R19_member = R19_method;  // MemberName ptr; incoming method ptr is dead now
 283     __ ld(R19_member, RegisterOrConstant((intptr_t)8), argbase);
 284     __ add(argbase, Interpreter::stackElementSize, argbase);
 285     generate_method_handle_dispatch(_masm, iid, tmp_recv, R19_member, not_for_compiler_entry);
 286   }
 287 
 288   return entry_point;
 289 }
 290 
 291 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
 292                                                     vmIntrinsics::ID iid,
 293                                                     Register receiver_reg,
 294                                                     Register member_reg,
 295                                                     bool for_compiler_entry) {
 296   assert(is_signature_polymorphic(iid), "expected invoke iid");
 297   Register temp1 = (for_compiler_entry ? R25_tmp5 : R7);
 298   Register temp2 = (for_compiler_entry ? R22_tmp2 : R8);
 299   Register temp3 = (for_compiler_entry ? R23_tmp3 : R9);
 300   Register temp4 = (for_compiler_entry ? R24_tmp4 : R10);
 301   if (receiver_reg != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
 302   if (member_reg   != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
 303 
 304   if (iid == vmIntrinsics::_invokeBasic) {
 305     // indirect through MH.form.vmentry.vmtarget
 306     jump_to_lambda_form(_masm, receiver_reg, R19_method, temp1, temp2, for_compiler_entry);
 307   } else {
 308     // The method is a member invoker used by direct method handles.
 309     if (VerifyMethodHandles) {
 310       // make sure the trailing argument really is a MemberName (caller responsibility)
 311       verify_klass(_masm, member_reg, SystemDictionary::WK_KLASS_ENUM_NAME(MemberName_klass),
 312                    temp1, temp2,
 313                    "MemberName required for invokeVirtual etc.");
 314     }
 315 
 316     Register temp1_recv_klass = temp1;
 317     if (iid != vmIntrinsics::_linkToStatic) {
 318       __ verify_oop(receiver_reg);
 319       if (iid == vmIntrinsics::_linkToSpecial) {
 320         // Don't actually load the klass; just null-check the receiver.
 321         __ null_check_throw(receiver_reg, -1, temp1,
 322                             Interpreter::throw_NullPointerException_entry());
 323       } else {
 324         // load receiver klass itself
 325         __ null_check_throw(receiver_reg, oopDesc::klass_offset_in_bytes(), temp1,
 326                             Interpreter::throw_NullPointerException_entry());
 327         __ load_klass(temp1_recv_klass, receiver_reg);
 328         __ verify_klass_ptr(temp1_recv_klass);
 329       }
 330       BLOCK_COMMENT("check_receiver {");
 331       // The receiver for the MemberName must be in receiver_reg.
 332       // Check the receiver against the MemberName.clazz
 333       if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
 334         // Did not load it above...
 335         __ load_klass(temp1_recv_klass, receiver_reg);
 336         __ verify_klass_ptr(temp1_recv_klass);
 337       }
 338       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
 339         Label L_ok;
 340         Register temp2_defc = temp2;
 341         __ load_heap_oop_not_null(temp2_defc, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()), member_reg, temp3);
 342         load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
 343         __ verify_klass_ptr(temp2_defc);
 344         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
 345         // If we get here, the type check failed!
 346         __ stop("receiver class disagrees with MemberName.clazz");
 347         __ BIND(L_ok);
 348       }
 349       BLOCK_COMMENT("} check_receiver");
 350     }
 351     if (iid == vmIntrinsics::_linkToSpecial ||
 352         iid == vmIntrinsics::_linkToStatic) {
 353       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
 354     }
 355 
 356     // Live registers at this point:
 357     //  member_reg - MemberName that was the trailing argument
 358     //  temp1_recv_klass - klass of stacked receiver, if needed
 359     //  O5_savedSP - interpreter linkage (if interpreted)
 360     //  O0..O5 - compiler arguments (if compiled)
 361 
 362     Label L_incompatible_class_change_error;
 363     switch (iid) {
 364     case vmIntrinsics::_linkToSpecial:
 365       if (VerifyMethodHandles) {
 366         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp2);
 367       }
 368       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes()), member_reg);
 369       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes()), R19_method);
 370       break;
 371 
 372     case vmIntrinsics::_linkToStatic:
 373       if (VerifyMethodHandles) {
 374         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp2);
 375       }
 376       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset_in_bytes()), member_reg);
 377       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset_in_bytes()), R19_method);
 378       break;
 379 
 380     case vmIntrinsics::_linkToVirtual:
 381     {
 382       // same as TemplateTable::invokevirtual,
 383       // minus the CP setup and profiling:
 384 
 385       if (VerifyMethodHandles) {
 386         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp2);
 387       }
 388 
 389       // pick out the vtable index from the MemberName, and then we can discard it:
 390       Register temp2_index = temp2;
 391       __ ld(temp2_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()), member_reg);
 392 
 393       if (VerifyMethodHandles) {
 394         Label L_index_ok;
 395         __ cmpdi(CCR1, temp2_index, 0);
 396         __ bge(CCR1, L_index_ok);
 397         __ stop("no virtual index");
 398         __ BIND(L_index_ok);
 399       }
 400 
 401       // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
 402       // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
 403 
 404       // get target Method* & entry point
 405       __ lookup_virtual_method(temp1_recv_klass, temp2_index, R19_method);
 406       break;
 407     }
 408 
 409     case vmIntrinsics::_linkToInterface:
 410     {
 411       // same as TemplateTable::invokeinterface
 412       // (minus the CP setup and profiling, with different argument motion)
 413       if (VerifyMethodHandles) {
 414         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp2);
 415       }
 416 
 417       Register temp2_intf = temp2;
 418       __ load_heap_oop_not_null(temp2_intf, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()), member_reg, temp3);
 419       load_klass_from_Class(_masm, temp2_intf, temp3, temp4);
 420       __ verify_klass_ptr(temp2_intf);
 421 
 422       Register vtable_index = R19_method;
 423       __ ld(vtable_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()), member_reg);
 424       if (VerifyMethodHandles) {
 425         Label L_index_ok;
 426         __ cmpdi(CCR1, vtable_index, 0);
 427         __ bge(CCR1, L_index_ok);
 428         __ stop("invalid vtable index for MH.invokeInterface");
 429         __ BIND(L_index_ok);
 430       }
 431 
 432       // given intf, index, and recv klass, dispatch to the implementation method
 433       __ lookup_interface_method(temp1_recv_klass, temp2_intf,
 434                                  // note: next two args must be the same:
 435                                  vtable_index, R19_method,
 436                                  temp3, temp4,
 437                                  L_incompatible_class_change_error);
 438       break;
 439     }
 440 
 441     default:
 442       fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid));
 443       break;
 444     }
 445 
 446     // Live at this point:
 447     //   R19_method
 448     //   O5_savedSP (if interpreted)
 449 
 450     // After figuring out which concrete method to call, jump into it.
 451     // Note that this works in the interpreter with no data motion.
 452     // But the compiled version will require that rcx_recv be shifted out.
 453     __ verify_method_ptr(R19_method);
 454     jump_from_method_handle(_masm, R19_method, temp1, temp2, for_compiler_entry);
 455 
 456     if (iid == vmIntrinsics::_linkToInterface) {
 457       __ BIND(L_incompatible_class_change_error);
 458       __ load_const_optimized(temp1, StubRoutines::throw_IncompatibleClassChangeError_entry());
 459       __ mtctr(temp1);
 460       __ bctr();
 461     }
 462   }
 463 }
 464 
 465 #ifndef PRODUCT
 466 void trace_method_handle_stub(const char* adaptername,
 467                               oopDesc* mh,
 468                               intptr_t* entry_sp,
 469                               intptr_t* saved_regs) {
 470 
 471   bool has_mh = (strstr(adaptername, "/static") == NULL &&
 472                  strstr(adaptername, "linkTo") == NULL);    // static linkers don't have MH
 473   const char* mh_reg_name = has_mh ? "R23_method_handle" : "G23";
 474   tty->print_cr("MH %s %s=" INTPTR_FORMAT " sp=" INTPTR_FORMAT,
 475                 adaptername, mh_reg_name, p2i(mh), p2i(entry_sp));
 476 
 477   if (Verbose) {
 478     tty->print_cr("Registers:");
 479     const int abi_offset = frame::abi_reg_args_size / 8;
 480     for (int i = R3->encoding(); i <= R12->encoding(); i++) {
 481       Register r = as_Register(i);
 482       int count = i - R3->encoding();
 483       // The registers are stored in reverse order on the stack (by save_volatile_gprs(R1_SP, abi_reg_args_size)).
 484       tty->print("%3s=" PTR_FORMAT, r->name(), saved_regs[abi_offset + count]);
 485       if ((count + 1) % 4 == 0) {
 486         tty->cr();
 487       } else {
 488         tty->print(", ");
 489       }
 490     }
 491     tty->cr();
 492 
 493     {
 494       // dumping last frame with frame::describe
 495 
 496       JavaThread* p = JavaThread::active();
 497 
 498       ResourceMark rm;
 499       PRESERVE_EXCEPTION_MARK; // may not be needed by safer and unexpensive here
 500       FrameValues values;
 501 
 502       // Note: We want to allow trace_method_handle from any call site.
 503       // While trace_method_handle creates a frame, it may be entered
 504       // without a PC on the stack top (e.g. not just after a call).
 505       // Walking that frame could lead to failures due to that invalid PC.
 506       // => carefully detect that frame when doing the stack walking
 507 
 508       // Current C frame
 509       frame cur_frame = os::current_frame();
 510 
 511       // Robust search of trace_calling_frame (independant of inlining).
 512       assert(cur_frame.sp() <= saved_regs, "registers not saved on stack ?");
 513       frame trace_calling_frame = os::get_sender_for_C_frame(&cur_frame);
 514       while (trace_calling_frame.fp() < saved_regs) {
 515         trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame);
 516       }
 517 
 518       // Safely create a frame and call frame::describe.
 519       intptr_t *dump_sp = trace_calling_frame.sender_sp();
 520 
 521       frame dump_frame = frame(dump_sp);
 522       dump_frame.describe(values, 1);
 523 
 524       values.describe(-1, saved_regs, "raw top of stack");
 525 
 526       tty->print_cr("Stack layout:");
 527       values.print(p);
 528     }
 529 
 530     if (has_mh && oopDesc::is_oop(mh)) {
 531       mh->print();
 532       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
 533         if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
 534           java_lang_invoke_MethodHandle::form(mh)->print();
 535       }
 536     }
 537   }
 538 }
 539 
 540 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 541   if (!TraceMethodHandles) return;
 542 
 543   BLOCK_COMMENT("trace_method_handle {");
 544 
 545   const Register tmp = R11; // Will be preserved.
 546   const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
 547   __ save_volatile_gprs(R1_SP, -nbytes_save); // except R0
 548   __ save_LR_CR(tmp); // save in old frame
 549 
 550   __ mr(R5_ARG3, R1_SP);     // saved_sp
 551   __ push_frame_reg_args(nbytes_save, tmp);
 552 
 553   __ load_const_optimized(R3_ARG1, (address)adaptername, tmp);
 554   __ mr(R4_ARG2, R23_method_handle);
 555   __ mr(R6_ARG4, R1_SP);
 556   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
 557 
 558   __ pop_frame();
 559   __ restore_LR_CR(tmp);
 560   __ restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
 561 
 562   BLOCK_COMMENT("} trace_method_handle");
 563 }
 564 #endif // PRODUCT