1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // This file mirror as much as possible methodHandles_x86.cpp to ease
  26 // cross platform development for JSR292.
  27 // Last synchronization: changeset f8c9417e3571
  28 
  29 #include "precompiled.hpp"
  30 #include "classfile/javaClasses.inline.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "memory/allocation.inline.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "prims/methodHandles.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 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp1, Register temp2) {
  48   if (VerifyMethodHandles) {
  49     verify_klass(_masm, klass_reg, temp1, temp2, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_Class),
  50                  "MH argument is a Class");
  51   }
  52   __ ldr(klass_reg, Address(klass_reg, java_lang_Class::klass_offset_in_bytes()));
  53 }
  54 
  55 #ifdef ASSERT
  56 static int check_nonzero(const char* xname, int x) {
  57   assert(x != 0, "%s should be nonzero", xname);
  58   return x;
  59 }
  60 #define NONZERO(x) check_nonzero(#x, x)
  61 #else //ASSERT
  62 #define NONZERO(x) (x)
  63 #endif //ASSERT
  64 
  65 #ifdef ASSERT
  66 void MethodHandles::verify_klass(MacroAssembler* _masm,
  67                                  Register obj, Register temp1, Register temp2, SystemDictionary::WKID klass_id,
  68                                  const char* error_message) {
  69   InstanceKlass** klass_addr = SystemDictionary::well_known_klass_addr(klass_id);
  70   KlassHandle klass = SystemDictionary::well_known_klass(klass_id);
  71   Label L_ok, L_bad;
  72   BLOCK_COMMENT("verify_klass {");
  73   __ verify_oop(obj);
  74   __ cbz(obj, L_bad);
  75   __ load_klass(temp1, obj);
  76   __ lea(temp2, ExternalAddress((address) klass_addr));
  77   __ ldr(temp2, temp2); // the cmpptr on x86 dereferences the AddressLiteral (not lea)
  78   __ cmp(temp1, temp2);
  79   __ b(L_ok, eq);
  80   intptr_t super_check_offset = klass->super_check_offset();
  81   __ ldr(temp1, Address(temp1, super_check_offset));
  82   __ cmp(temp1, temp2);
  83   __ b(L_ok, eq);
  84 
  85   __ bind(L_bad);
  86   __ stop(error_message);
  87   __ BIND(L_ok);
  88   BLOCK_COMMENT("} verify_klass");
  89 }
  90 
  91 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {
  92   Label L;
  93   BLOCK_COMMENT("verify_ref_kind {");
  94   __ ldr_u32(temp, Address(member_reg, NONZERO(java_lang_invoke_MemberName::flags_offset_in_bytes())));
  95   __ logical_shift_right(temp, temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
  96   __ andr(temp, temp, (unsigned)java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
  97   __ cmp(temp, ref_kind);
  98   __ b(L, eq);
  99   { char* buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
 100   jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind);
 101   if (ref_kind == JVM_REF_invokeVirtual ||
 102       ref_kind == JVM_REF_invokeSpecial)
 103     // could do this for all ref_kinds, but would explode assembly code size
 104     trace_method_handle(_masm, buf);
 105   __ stop(buf);
 106   }
 107   BLOCK_COMMENT("} verify_ref_kind");
 108   __ bind(L);
 109 }
 110 
 111 #endif //ASSERT
 112 
 113 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, bool for_compiler_entry) {
 114   Label L_no_such_method;
 115   __ cbz(Rmethod, L_no_such_method);
 116 
 117   // Note: JVMTI overhead seems small enough compared to invocation
 118   // cost and is not worth the complexity or code size overhead of
 119   // supporting several variants of each adapter.
 120   if (!for_compiler_entry && (JvmtiExport::can_post_interpreter_events())) {
 121     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 122     // compiled code in threads for which the event is enabled.  Check here for
 123     // interp_only_mode if these events CAN be enabled.
 124     __ ldr_s32(Rtemp, Address(Rthread, JavaThread::interp_only_mode_offset()));
 125 #ifdef AARCH64
 126     Label L;
 127     __ cbz(Rtemp, L);
 128     __ indirect_jump(Address(Rmethod, Method::interpreter_entry_offset()), Rtemp);
 129     __ bind(L);
 130 #else
 131     __ cmp(Rtemp, 0);
 132     __ ldr(PC, Address(Rmethod, Method::interpreter_entry_offset()), ne);
 133 #endif // AARCH64
 134   }
 135   const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
 136                                                      Method::from_interpreted_offset();
 137 
 138   __ indirect_jump(Address(Rmethod, entry_offset), Rtemp);
 139 
 140   __ bind(L_no_such_method);
 141   // throw exception
 142   __ jump(StubRoutines::throw_AbstractMethodError_entry(), relocInfo::runtime_call_type, Rtemp);
 143 }
 144 
 145 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
 146                                         Register recv, Register tmp,
 147                                         bool for_compiler_entry) {
 148   BLOCK_COMMENT("jump_to_lambda_form {");
 149   // This is the initial entry point of a lazy method handle.
 150   // After type checking, it picks up the invoker from the LambdaForm.
 151   assert_different_registers(recv, tmp, Rmethod);
 152 
 153   // Load the invoker, as MH -> MH.form -> LF.vmentry
 154   __ load_heap_oop(tmp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes())));
 155   __ verify_oop(tmp);
 156 
 157   __ load_heap_oop(tmp, Address(tmp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes())));
 158   __ verify_oop(tmp);
 159 
 160   // the following assumes that a Method* is normally compressed in the vmtarget field:
 161   __ ldr(Rmethod, Address(tmp, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes())));
 162 
 163   if (VerifyMethodHandles && !for_compiler_entry) {
 164     // make sure recv is already on stack
 165     __ ldr(tmp, Address(Rmethod, Method::const_offset()));
 166     __ load_sized_value(tmp,
 167                         Address(tmp, ConstMethod::size_of_parameters_offset()),
 168                         sizeof(u2), /*is_signed*/ false);
 169     // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
 170     Label L;
 171     __ ldr(tmp, __ receiver_argument_address(Rparams, tmp, tmp));
 172     __ cmp(tmp, recv);
 173     __ b(L, eq);
 174     __ stop("receiver not on stack");
 175     __ bind(L);
 176   }
 177 
 178   jump_from_method_handle(_masm, for_compiler_entry);
 179   BLOCK_COMMENT("} jump_to_lambda_form");
 180 }
 181 
 182 
 183 // Code generation
 184 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
 185                                                                 vmIntrinsics::ID iid) {
 186   const bool not_for_compiler_entry = false;  // this is the interpreter entry
 187   assert(is_signature_polymorphic(iid), "expected invoke iid");
 188   if (iid == vmIntrinsics::_invokeGeneric ||
 189       iid == vmIntrinsics::_compiledLambdaForm) {
 190     // Perhaps surprisingly, the user-visible names, and linkToCallSite, are not directly used.
 191     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
 192     // They all require an extra argument.
 193     __ should_not_reach_here();           // empty stubs make SG sick
 194     return NULL;
 195   }
 196 
 197   // Rmethod: Method*
 198   // Rparams (SP on 32-bit ARM): pointer to parameters
 199   // Rsender_sp (R4/R19): sender SP (must preserve; see prepare_to_jump_from_interpreted)
 200   // R5_mh: receiver method handle (must load from sp[MethodTypeForm.vmslots])
 201   // R1, R2, Rtemp: garbage temp, blown away
 202 
 203   // Use same name as x86 to ease future merges
 204   Register rdx_temp       = R2_tmp;
 205   Register rdx_param_size = rdx_temp;  // size of parameters
 206   Register rax_temp       = R1_tmp;
 207   Register rcx_mh         = R5_mh;     // MH receiver; dies quickly and is recycled
 208   Register rbx_method     = Rmethod;   // eventual target of this invocation
 209   Register rdi_temp       = Rtemp;
 210 
 211   // here's where control starts out:
 212   __ align(CodeEntryAlignment);
 213   address entry_point = __ pc();
 214 
 215   if (VerifyMethodHandles) {
 216     Label L;
 217     BLOCK_COMMENT("verify_intrinsic_id {");
 218     __ ldrh(rdi_temp, Address(rbx_method, Method::intrinsic_id_offset_in_bytes()));
 219     __ sub_slow(rdi_temp, rdi_temp, (int) iid);
 220     __ cbz(rdi_temp, L);
 221     if (iid == vmIntrinsics::_linkToVirtual ||
 222         iid == vmIntrinsics::_linkToSpecial) {
 223       // could do this for all kinds, but would explode assembly code size
 224       trace_method_handle(_masm, "bad Method*::intrinsic_id");
 225     }
 226     __ stop("bad Method*::intrinsic_id");
 227     __ bind(L);
 228     BLOCK_COMMENT("} verify_intrinsic_id");
 229   }
 230 
 231   // First task:  Find out how big the argument list is.
 232   Address rdx_first_arg_addr;
 233   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
 234   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
 235   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
 236     __ ldr(rdx_param_size, Address(rbx_method, Method::const_offset()));
 237     __ load_sized_value(rdx_param_size,
 238                         Address(rdx_param_size, ConstMethod::size_of_parameters_offset()),
 239                         sizeof(u2), /*is_signed*/ false);
 240     // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
 241     rdx_first_arg_addr = __ receiver_argument_address(Rparams, rdx_param_size, rdi_temp);
 242   } else {
 243     DEBUG_ONLY(rdx_param_size = noreg);
 244   }
 245 
 246   if (!is_signature_polymorphic_static(iid)) {
 247     __ ldr(rcx_mh, rdx_first_arg_addr);
 248     DEBUG_ONLY(rdx_param_size = noreg);
 249   }
 250 
 251   // rdx_first_arg_addr is live!
 252 
 253   trace_method_handle_interpreter_entry(_masm, iid);
 254 
 255   if (iid == vmIntrinsics::_invokeBasic) {
 256     generate_method_handle_dispatch(_masm, iid, rcx_mh, noreg, not_for_compiler_entry);
 257 
 258   } else {
 259     // Adjust argument list by popping the trailing MemberName argument.
 260     Register rcx_recv = noreg;
 261     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
 262       // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
 263       __ ldr(rcx_recv = rcx_mh, rdx_first_arg_addr);
 264       DEBUG_ONLY(rdx_param_size = noreg);
 265     }
 266     Register rbx_member = rbx_method;  // MemberName ptr; incoming method ptr is dead now
 267 #ifdef AARCH64
 268     __ ldr(rbx_member, Address(Rparams, Interpreter::stackElementSize, post_indexed));
 269 #else
 270     __ pop(rbx_member);
 271 #endif
 272     generate_method_handle_dispatch(_masm, iid, rcx_recv, rbx_member, not_for_compiler_entry);
 273   }
 274   return entry_point;
 275 }
 276 
 277 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
 278                                                     vmIntrinsics::ID iid,
 279                                                     Register receiver_reg,
 280                                                     Register member_reg,
 281                                                     bool for_compiler_entry) {
 282   assert(is_signature_polymorphic(iid), "expected invoke iid");
 283   // Use same name as x86 to ease future merges
 284   Register rbx_method = Rmethod;   // eventual target of this invocation
 285   // temps used in this code are not used in *either* compiled or interpreted calling sequences
 286   Register temp1 = (for_compiler_entry ? saved_last_sp_register() : R1_tmp);
 287   Register temp2 = AARCH64_ONLY(R9) NOT_AARCH64(R8);
 288   Register temp3 = Rtemp; // R12/R16
 289   Register temp4 = AARCH64_ONLY(Rtemp2) NOT_AARCH64(R5);
 290   if (for_compiler_entry) {
 291     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : j_rarg0), "only valid assignment");
 292 #ifdef AARCH64
 293     assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 294     assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 295     assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 296     assert_different_registers(temp4, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 297 #else
 298     assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3);
 299     assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3);
 300     assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3);
 301     assert_different_registers(temp4, j_rarg0, j_rarg1, j_rarg2, j_rarg3);
 302 #endif // AARCH64
 303   }
 304   assert_different_registers(temp1, temp2, temp3, receiver_reg);
 305   assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
 306   if (!for_compiler_entry)
 307     assert_different_registers(temp1, temp2, temp3, temp4, saved_last_sp_register());  // don't trash lastSP
 308 
 309   if (iid == vmIntrinsics::_invokeBasic) {
 310     // indirect through MH.form.exactInvoker.vmtarget
 311     jump_to_lambda_form(_masm, receiver_reg, temp3, for_compiler_entry);
 312 
 313   } else {
 314     // The method is a member invoker used by direct method handles.
 315     if (VerifyMethodHandles) {
 316       // make sure the trailing argument really is a MemberName (caller responsibility)
 317       verify_klass(_masm, member_reg, temp2, temp3, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_invoke_MemberName),
 318                    "MemberName required for invokeVirtual etc.");
 319     }
 320 
 321     Address member_clazz(   member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
 322     Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
 323     Address member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes()));
 324 
 325     Register temp1_recv_klass = temp1;
 326     if (iid != vmIntrinsics::_linkToStatic) {
 327       if (iid == vmIntrinsics::_linkToSpecial) {
 328         // Don't actually load the klass; just null-check the receiver.
 329         __ null_check(receiver_reg, temp3);
 330       } else {
 331         // load receiver klass itself
 332         __ null_check(receiver_reg, temp3, oopDesc::klass_offset_in_bytes());
 333         __ load_klass(temp1_recv_klass, receiver_reg);
 334         __ verify_klass_ptr(temp1_recv_klass);
 335       }
 336       BLOCK_COMMENT("check_receiver {");
 337       // The receiver for the MemberName must be in receiver_reg.
 338       // Check the receiver against the MemberName.clazz
 339       if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
 340         // Did not load it above...
 341         __ load_klass(temp1_recv_klass, receiver_reg);
 342         __ verify_klass_ptr(temp1_recv_klass);
 343       }
 344       // Check the receiver against the MemberName.clazz
 345       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
 346         Label L_ok;
 347         Register temp2_defc = temp2;
 348         __ load_heap_oop(temp2_defc, member_clazz);
 349         load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
 350         __ verify_klass_ptr(temp2_defc);
 351 #ifdef AARCH64
 352         // TODO-AARCH64
 353         __ b(L_ok);
 354 #else
 355         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, noreg, L_ok);
 356 #endif
 357         // If we get here, the type check failed!
 358         __ stop("receiver class disagrees with MemberName.clazz");
 359         __ bind(L_ok);
 360       }
 361       BLOCK_COMMENT("} check_receiver");
 362     }
 363     if (iid == vmIntrinsics::_linkToSpecial ||
 364         iid == vmIntrinsics::_linkToStatic) {
 365       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
 366     }
 367 
 368     // Live registers at this point:
 369     //  member_reg - MemberName that was the extra argument
 370     //  temp1_recv_klass - klass of stacked receiver, if needed
 371 
 372     Label L_incompatible_class_change_error;
 373     switch (iid) {
 374     case vmIntrinsics::_linkToSpecial:
 375       if (VerifyMethodHandles) {
 376         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
 377       }
 378       __ ldr(Rmethod, member_vmtarget);
 379       break;
 380 
 381     case vmIntrinsics::_linkToStatic:
 382       if (VerifyMethodHandles) {
 383         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
 384       }
 385       __ ldr(Rmethod, member_vmtarget);
 386       break;
 387 
 388     case vmIntrinsics::_linkToVirtual:
 389     {
 390       // same as TemplateTable::invokevirtual,
 391       // minus the CP setup and profiling:
 392 
 393       if (VerifyMethodHandles) {
 394         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
 395       }
 396 
 397       // pick out the vtable index from the MemberName, and then we can discard it:
 398       Register temp2_index = temp2;
 399       __ ldr(temp2_index, member_vmindex);
 400 
 401       if (VerifyMethodHandles) {
 402         Label L_index_ok;
 403         __ cmp(temp2_index, 0);
 404         __ b(L_index_ok, ge);
 405         __ stop("no virtual index");
 406         __ bind(L_index_ok);
 407       }
 408 
 409       // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
 410       // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
 411 
 412       // get target Method* & entry point
 413       __ lookup_virtual_method(temp1_recv_klass, temp2_index, Rmethod);
 414       break;
 415     }
 416 
 417     case vmIntrinsics::_linkToInterface:
 418     {
 419       // same as TemplateTable::invokeinterface
 420       // (minus the CP setup and profiling, with different argument motion)
 421       if (VerifyMethodHandles) {
 422         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
 423       }
 424 
 425       Register temp3_intf = temp3;
 426       __ load_heap_oop(temp3_intf, member_clazz);
 427       load_klass_from_Class(_masm, temp3_intf, temp2, temp4);
 428       __ verify_klass_ptr(temp3_intf);
 429 
 430       Register rbx_index = rbx_method;
 431       __ ldr(rbx_index, member_vmindex);
 432       if (VerifyMethodHandles) {
 433         Label L;
 434         __ cmp(rbx_index, 0);
 435         __ b(L, ge);
 436         __ stop("invalid vtable index for MH.invokeInterface");
 437         __ bind(L);
 438       }
 439 
 440       // given intf, index, and recv klass, dispatch to the implementation method
 441       Label L_no_such_interface;
 442       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
 443                                  // note: next two args must be the same:
 444                                  rbx_index, rbx_method,
 445                                  temp2, temp4,
 446                                  L_incompatible_class_change_error);
 447       break;
 448     }
 449 
 450     default:
 451       fatal("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid));
 452       break;
 453     }
 454 
 455     // Live at this point:
 456     //   Rmethod (target method)
 457     //   Rsender_sp, Rparams (if interpreted)
 458     //   register arguments (if compiled)
 459 
 460     // After figuring out which concrete method to call, jump into it.
 461     __ verify_method_ptr(Rmethod);
 462     jump_from_method_handle(_masm, for_compiler_entry);
 463 
 464     if (iid == vmIntrinsics::_linkToInterface) {
 465       __ bind(L_incompatible_class_change_error);
 466       __ jump(StubRoutines::throw_IncompatibleClassChangeError_entry(), relocInfo::runtime_call_type, Rtemp);
 467     }
 468   }
 469 }
 470 
 471 
 472 #ifndef PRODUCT
 473 enum {
 474   ARG_LIMIT = 255, SLOP = 4,
 475   // use this parameter for checking for garbage stack movements:
 476   UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
 477   // the slop defends against false alarms due to fencepost errors
 478 };
 479 
 480 #ifdef AARCH64
 481 const int trace_mh_nregs = 32; // R0-R30, PC
 482 #else
 483 const int trace_mh_nregs = 15;
 484 const Register trace_mh_regs[trace_mh_nregs] =
 485   {R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, LR, PC};
 486 #endif // AARCH64
 487 
 488 void trace_method_handle_stub(const char* adaptername,
 489                               intptr_t* saved_regs,
 490                               intptr_t* saved_bp,
 491                               oop mh) {
 492   // called as a leaf from native code: do not block the JVM!
 493   bool has_mh = (strstr(adaptername, "/static") == NULL &&
 494                  strstr(adaptername, "linkTo") == NULL);    // static linkers don't have MH
 495   intptr_t* entry_sp = (intptr_t*) &saved_regs[trace_mh_nregs]; // just after the saved regs
 496   intptr_t* saved_sp = (intptr_t*)  saved_regs[Rsender_sp->encoding()]; // save of Rsender_sp
 497   intptr_t* last_sp  = (intptr_t*)  saved_bp[AARCH64_ONLY(frame::interpreter_frame_stack_top_offset) NOT_AARCH64(frame::interpreter_frame_last_sp_offset)];
 498   intptr_t* base_sp  = last_sp;
 499 
 500   intptr_t    mh_reg = (intptr_t)saved_regs[R5_mh->encoding()];
 501   const char* mh_reg_name = "R5_mh";
 502   if (!has_mh)  mh_reg_name = "R5";
 503   tty->print_cr("MH %s %s=" PTR_FORMAT " sp=(" PTR_FORMAT "+" INTX_FORMAT ") stack_size=" INTX_FORMAT " bp=" PTR_FORMAT,
 504                 adaptername, mh_reg_name, mh_reg,
 505                 (intptr_t)entry_sp, (intptr_t)saved_sp - (intptr_t)entry_sp, (intptr_t)(base_sp - last_sp), (intptr_t)saved_bp);
 506 
 507   if (last_sp != saved_sp && last_sp != NULL)
 508     tty->print_cr("*** last_sp=" INTPTR_FORMAT, p2i(last_sp));
 509   if (Verbose) {
 510     tty->print(" reg dump: ");
 511     int i;
 512     for (i = 0; i < trace_mh_nregs; i++) {
 513       if (i > 0 && i % AARCH64_ONLY(2) NOT_AARCH64(4) == 0)
 514         tty->print("\n   + dump: ");
 515 #ifdef AARCH64
 516       const char* reg_name = (i == trace_mh_nregs-1) ? "pc" : as_Register(i)->name();
 517 #else
 518       const char* reg_name = trace_mh_regs[i]->name();
 519 #endif
 520       tty->print(" %s: " INTPTR_FORMAT, reg_name, p2i((void *)saved_regs[i]));
 521     }
 522     tty->cr();
 523   }
 524 
 525   if (Verbose) {
 526     // dump last frame (from JavaThread::print_frame_layout)
 527 
 528     // Note: code is robust but the dumped informationm may not be
 529     // 100% correct, particularly with respect to the dumped
 530     // "unextended_sp". Getting it right for all trace_method_handle
 531     // call paths is not worth the complexity/risk. The correct slot
 532     // will be identified by *Rsender_sp anyway in the dump.
 533     JavaThread* p = JavaThread::active();
 534 
 535     ResourceMark rm;
 536     PRESERVE_EXCEPTION_MARK;
 537     FrameValues values;
 538 
 539     intptr_t* dump_fp = (intptr_t *) saved_bp;
 540     address dump_pc = (address) saved_regs[trace_mh_nregs-2]; // LR (with LR,PC last in saved_regs)
 541     frame dump_frame((intptr_t *)entry_sp, dump_fp, dump_pc);
 542 
 543     dump_frame.describe(values, 1);
 544     // mark Rsender_sp if seems valid
 545     if (has_mh) {
 546       if ((saved_sp >= entry_sp - UNREASONABLE_STACK_MOVE) && (saved_sp < dump_fp)) {
 547         values.describe(-1, saved_sp, "*Rsender_sp");
 548       }
 549     }
 550 
 551     // Note: the unextended_sp may not be correct
 552     tty->print_cr("  stack layout:");
 553     values.print(p);
 554   }
 555   if (Verbose) {
 556     if (has_mh && mh->is_oop()) {
 557       mh->print();
 558       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
 559         if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
 560           java_lang_invoke_MethodHandle::form(mh)->print();
 561       }
 562     }
 563   }
 564 }
 565 
 566 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 567   if (!TraceMethodHandles)  return;
 568   BLOCK_COMMENT("trace_method_handle {");
 569   // register saving
 570   //  must correspond to trace_mh_nregs and trace_mh_regs defined above
 571   int push_size = __ save_all_registers();
 572   assert(trace_mh_nregs*wordSize == push_size,"saved register count mismatch");
 573 
 574   __ mov_slow(R0, adaptername);
 575   __ mov(R1, SP); // entry_sp (after pushes)
 576   __ mov(R2, FP);
 577   if (R5_mh != R3) {
 578     assert_different_registers(R0, R1, R2, R5_mh);
 579     __ mov(R3, R5_mh);
 580   }
 581 
 582   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub), R0, R1, R2, R3);
 583 
 584   __ restore_all_registers();
 585   BLOCK_COMMENT("} trace_method_handle");
 586 }
 587 #endif //PRODUCT