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