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::generate_method_handle_dispatch(MacroAssembler* _masm,
312                                                     vmIntrinsics::ID iid,
313                                                     Register receiver_reg,
314                                                     Register member_reg,
315                                                     bool for_compiler_entry) {
316   assert(is_signature_polymorphic(iid), "expected invoke iid");
317   Register temp1 = (for_compiler_entry ? R25_tmp5 : R31); // must be non-volatile due to runtime calls
318   Register temp2 = (for_compiler_entry ? R22_tmp2 : R8);
319   Register temp3 = (for_compiler_entry ? R23_tmp3 : R9);
320   Register temp4 = (for_compiler_entry ? R24_tmp4 : R10);
321   if (receiver_reg != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
322   if (member_reg   != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
323 
324   const MacroAssembler::PreservationLevel preservation_level = for_compiler_entry
325     ? MacroAssembler::PRESERVATION_FRAME_LR_GP_FP_REGS
326     : MacroAssembler::PRESERVATION_FRAME_LR;
327 
328   if (iid == vmIntrinsics::_invokeBasic || iid == vmIntrinsics::_linkToNative) {
329     if (iid == vmIntrinsics::_linkToNative) {
330       assert(for_compiler_entry, "only compiler entry is supported");
331     }
332     // indirect through MH.form.vmentry.vmtarget
333     jump_to_lambda_form(_masm, receiver_reg, R19_method, temp1, temp2, for_compiler_entry);
334   } else {
335     // The method is a member invoker used by direct method handles.
336     if (VerifyMethodHandles) {
337       // make sure the trailing argument really is a MemberName (caller responsibility)
338       verify_klass(_masm, member_reg, VM_CLASS_ID(MemberName_klass),
339                    temp1, temp2,
340                    "MemberName required for invokeVirtual etc.");
341     }
342 
343     Register temp1_recv_klass = temp1;
344     if (iid != vmIntrinsics::_linkToStatic) {
345       BLOCK_COMMENT("check_receiver {");
346       __ verify_oop(receiver_reg, FILE_AND_LINE);
347 
348       const int klass_offset = iid == vmIntrinsics::_linkToSpecial
349         ? -1                                  // enforce receiver null check
350         : oopDesc::klass_offset_in_bytes();   // regular null-checking behavior
351 
352       __ null_check_throw(receiver_reg, klass_offset, temp1, Interpreter::throw_NullPointerException_entry());
353 
354       if (iid != vmIntrinsics::_linkToSpecial || VerifyMethodHandles) {
355         __ load_klass(temp1_recv_klass, receiver_reg);
356         __ verify_klass_ptr(temp1_recv_klass);
357       }
358 
359       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
360         Label L_ok;
361         Register temp2_defc = temp2;
362 
363         __ load_heap_oop(temp2_defc, NONZERO(java_lang_invoke_MemberName::clazz_offset()), member_reg,
364                          temp3, temp4, preservation_level, IS_NOT_NULL);
365         load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
366         __ verify_klass_ptr(temp2_defc);
367         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
368         // If we get here, the type check failed!
369         __ stop("receiver class disagrees with MemberName.clazz");
370         __ BIND(L_ok);
371       }
372       BLOCK_COMMENT("} check_receiver");
373     }
374     if (iid == vmIntrinsics::_linkToSpecial ||
375         iid == vmIntrinsics::_linkToStatic) {
376       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
377     }
378 
379     // Live registers at this point:
380     //  member_reg - MemberName that was the trailing argument
381     //  temp1_recv_klass - klass of stacked receiver, if needed
382 
383     Label L_incompatible_class_change_error;
384     switch (iid) {
385     case vmIntrinsics::_linkToSpecial:
386       if (VerifyMethodHandles) {
387         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp2);
388       }
389       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset()), member_reg,
390                        temp3, temp4, preservation_level, IS_NOT_NULL);
391       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()), R19_method);
392       break;
393 
394     case vmIntrinsics::_linkToStatic:
395       if (VerifyMethodHandles) {
396         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp2);
397       }
398       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset()), member_reg,
399                        temp3, temp4, preservation_level, IS_NOT_NULL);
400       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()), R19_method);
401       break;
402 
403     case vmIntrinsics::_linkToVirtual:
404     {
405       // same as TemplateTable::invokevirtual,
406       // minus the CP setup and profiling:
407 
408       if (VerifyMethodHandles) {
409         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp2);
410       }
411 
412       // pick out the vtable index from the MemberName, and then we can discard it:
413       Register temp2_index = temp2;
414       __ ld(temp2_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset()), member_reg);
415 
416       if (VerifyMethodHandles) {
417         Label L_index_ok;
418         __ cmpdi(CCR1, temp2_index, 0);
419         __ bge(CCR1, L_index_ok);
420         __ stop("no virtual index");
421         __ BIND(L_index_ok);
422       }
423 
424       // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
425       // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
426 
427       // get target Method* & entry point
428       __ lookup_virtual_method(temp1_recv_klass, temp2_index, R19_method);
429       break;
430     }
431 
432     case vmIntrinsics::_linkToInterface:
433     {
434       // same as TemplateTable::invokeinterface
435       // (minus the CP setup and profiling, with different argument motion)
436       if (VerifyMethodHandles) {
437         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp2);
438       }
439 
440       Register temp2_intf = temp2;
441       __ load_heap_oop(temp2_intf, NONZERO(java_lang_invoke_MemberName::clazz_offset()), member_reg,
442                        temp3, temp4, preservation_level, IS_NOT_NULL);
443       load_klass_from_Class(_masm, temp2_intf, temp3, temp4);
444       __ verify_klass_ptr(temp2_intf);
445 
446       Register vtable_index = R19_method;
447       __ ld(vtable_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset()), member_reg);
448       if (VerifyMethodHandles) {
449         Label L_index_ok;
450         __ cmpdi(CCR1, vtable_index, 0);
451         __ bge(CCR1, L_index_ok);
452         __ stop("invalid vtable index for MH.invokeInterface");
453         __ BIND(L_index_ok);
454       }
455 
456       // given intf, index, and recv klass, dispatch to the implementation method
457       __ lookup_interface_method(temp1_recv_klass, temp2_intf,
458                                  // note: next two args must be the same:
459                                  vtable_index, R19_method,
460                                  temp3, temp4,
461                                  L_incompatible_class_change_error);
462       break;
463     }
464 
465     default:
466       fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
467       break;
468     }
469 
470     // Live at this point:
471     //   R19_method
472 
473     // After figuring out which concrete method to call, jump into it.
474     // Note that this works in the interpreter with no data motion.
475     // But the compiled version will require that rcx_recv be shifted out.
476     __ verify_method_ptr(R19_method);
477     jump_from_method_handle(_masm, R19_method, temp1, temp2, for_compiler_entry);
478 
479     if (iid == vmIntrinsics::_linkToInterface) {
480       __ BIND(L_incompatible_class_change_error);
481       __ load_const_optimized(temp1, StubRoutines::throw_IncompatibleClassChangeError_entry());
482       __ mtctr(temp1);
483       __ bctr();
484     }
485   }
486 }
487 
488 #ifndef PRODUCT
489 void trace_method_handle_stub(const char* adaptername,
490                               oopDesc* mh,
491                               intptr_t* entry_sp,
492                               intptr_t* saved_regs) {
493 
494   bool has_mh = (strstr(adaptername, "/static") == NULL &&
495                  strstr(adaptername, "linkTo") == NULL);    // static linkers don't have MH
496   const char* mh_reg_name = has_mh ? "R23_method_handle" : "G23";
497   log_info(methodhandles)("MH %s %s=" INTPTR_FORMAT " sp=" INTPTR_FORMAT,
498                 adaptername, mh_reg_name, p2i(mh), p2i(entry_sp));
499 
500   LogTarget(Trace, methodhandles) lt;
501   if (lt.is_enabled()) {
502     ResourceMark rm;
503     LogStream ls(lt);
504     ls.print_cr("Registers:");
505     const int abi_offset = frame::abi_reg_args_size / 8;
506     for (int i = R3->encoding(); i <= R12->encoding(); i++) {
507       Register r = as_Register(i);
508       int count = i - R3->encoding();
509       // The registers are stored in reverse order on the stack (by save_volatile_gprs(R1_SP, abi_reg_args_size)).
510       ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[abi_offset + count]);
511       if ((count + 1) % 4 == 0) {
512         ls.cr();
513       } else {
514         ls.print(", ");
515       }
516     }
517     ls.cr();
518 
519     {
520       // dumping last frame with frame::describe
521 
522       JavaThread* p = JavaThread::active();
523 
524       // may not be needed by safer and unexpensive here
525       PreserveExceptionMark pem(Thread::current());
526       FrameValues values;
527 
528       // Note: We want to allow trace_method_handle from any call site.
529       // While trace_method_handle creates a frame, it may be entered
530       // without a PC on the stack top (e.g. not just after a call).
531       // Walking that frame could lead to failures due to that invalid PC.
532       // => carefully detect that frame when doing the stack walking
533 
534       // Current C frame
535       frame cur_frame = os::current_frame();
536 
537       // Robust search of trace_calling_frame (independent of inlining).
538       assert(cur_frame.sp() <= saved_regs, "registers not saved on stack ?");
539       frame trace_calling_frame = os::get_sender_for_C_frame(&cur_frame);
540       while (trace_calling_frame.fp() < saved_regs) {
541         trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame);
542       }
543 
544       // Safely create a frame and call frame::describe.
545       intptr_t *dump_sp = trace_calling_frame.sender_sp();
546 
547       frame dump_frame = frame(dump_sp);
548       dump_frame.describe(values, 1);
549 
550       values.describe(-1, saved_regs, "raw top of stack");
551 
552       ls.print_cr("Stack layout:");
553       values.print_on(p, &ls);
554     }
555 
556     if (has_mh && oopDesc::is_oop(mh)) {
557       mh->print_on(&ls);
558       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
559         java_lang_invoke_MethodHandle::form(mh)->print_on(&ls);
560       }
561     }
562   }
563 }
564 
565 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
566   if (!log_is_enabled(Info, methodhandles)) return;
567 
568   BLOCK_COMMENT("trace_method_handle {");
569 
570   const Register tmp = R11; // Will be preserved.
571   const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
572   __ save_volatile_gprs(R1_SP, -nbytes_save); // except R0
573   __ save_LR_CR(tmp); // save in old frame
574 
575   __ mr(R5_ARG3, R1_SP);     // saved_sp
576   __ push_frame_reg_args(nbytes_save, tmp);
577 
578   __ load_const_optimized(R3_ARG1, (address)adaptername, tmp);
579   __ mr(R4_ARG2, R23_method_handle);
580   __ mr(R6_ARG4, R1_SP);
581   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
582 
583   __ pop_frame();
584   __ restore_LR_CR(tmp);
585   __ restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
586 
587   BLOCK_COMMENT("} trace_method_handle");
588 }
589 #endif // PRODUCT