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