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