methodHandles_sparc.cpp 24.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "asm/macroAssembler.hpp"
27 28 29
#include "interpreter/interpreter.hpp"
#include "memory/allocation.inline.hpp"
#include "prims/methodHandles.hpp"
30 31 32

#define __ _masm->

33 34
#ifdef PRODUCT
#define BLOCK_COMMENT(str) /* nothing */
35
#define STOP(error) stop(error)
36 37
#else
#define BLOCK_COMMENT(str) __ block_comment(str)
38
#define STOP(error) block_comment(error); __ stop(error)
39 40 41 42
#endif

#define BIND(label) bind(label); BLOCK_COMMENT(#label ":")

43 44 45
// Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
static RegisterOrConstant constant(int value) {
  return RegisterOrConstant(value);
46 47 48 49
}

void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
  if (VerifyMethodHandles)
50
    verify_klass(_masm, klass_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_Class), temp_reg, temp2_reg,
51
                 "MH argument is a Class");
52
  __ ld_ptr(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
53 54
}

55
#ifdef ASSERT
56 57 58
static int check_nonzero(const char* xname, int x) {
  assert(x != 0, err_msg("%s should be nonzero", xname));
  return x;
59
}
60 61 62 63
#define NONZERO(x) check_nonzero(#x, x)
#else //ASSERT
#define NONZERO(x) (x)
#endif //ASSERT
64 65 66

#ifdef ASSERT
void MethodHandles::verify_klass(MacroAssembler* _masm,
67
                                 Register obj_reg, SystemDictionary::WKID klass_id,
68 69
                                 Register temp_reg, Register temp2_reg,
                                 const char* error_message) {
70 71
  Klass** klass_addr = SystemDictionary::well_known_klass_addr(klass_id);
  KlassHandle klass = SystemDictionary::well_known_klass(klass_id);
72 73 74 75 76 77 78 79
  bool did_save = false;
  if (temp_reg == noreg || temp2_reg == noreg) {
    temp_reg = L1;
    temp2_reg = L2;
    __ save_frame_and_mov(0, obj_reg, L0);
    obj_reg = L0;
    did_save = true;
  }
80 81 82
  Label L_ok, L_bad;
  BLOCK_COMMENT("verify_klass {");
  __ verify_oop(obj_reg);
K
kvn 已提交
83
  __ br_null_short(obj_reg, Assembler::pn, L_bad);
84
  __ load_klass(obj_reg, temp_reg);
85
  __ set(ExternalAddress((Metadata**)klass_addr), temp2_reg);
86
  __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
K
kvn 已提交
87
  __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
88 89
  intptr_t super_check_offset = klass->super_check_offset();
  __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
90
  __ set(ExternalAddress((Metadata**)klass_addr), temp2_reg);
91
  __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
K
kvn 已提交
92
  __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
93
  __ BIND(L_bad);
94 95
  if (did_save)  __ restore();
  __ STOP(error_message);
96
  __ BIND(L_ok);
97
  if (did_save)  __ restore();
98 99
  BLOCK_COMMENT("} verify_klass");
}
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {
  Label L;
  BLOCK_COMMENT("verify_ref_kind {");
  __ lduw(Address(member_reg, NONZERO(java_lang_invoke_MemberName::flags_offset_in_bytes())), temp);
  __ srl( temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, temp);
  __ and3(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,  temp);
  __ cmp_and_br_short(temp, ref_kind, Assembler::equal, Assembler::pt, L);
  { char* buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
    jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind);
    if (ref_kind == JVM_REF_invokeVirtual ||
        ref_kind == JVM_REF_invokeSpecial)
      // could do this for all ref_kinds, but would explode assembly code size
      trace_method_handle(_masm, buf);
    __ STOP(buf);
  }
  BLOCK_COMMENT("} verify_ref_kind");
  __ bind(L);
}

#endif // ASSERT
121

122 123
void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp,
                                            bool for_compiler_entry) {
124
  assert(method == G5_method, "interpreter calling convention");
125
  assert_different_registers(method, target, temp);
126 127 128

  if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
    Label run_compiled_code;
129 130 131 132 133 134
    // JVMTI events, such as single-stepping, are implemented partly by avoiding running
    // compiled code in threads for which the event is enabled.  Check here for
    // interp_only_mode if these events CAN be enabled.
    __ verify_thread();
    const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
    __ ld(interp_only, temp);
135
    __ cmp_and_br_short(temp, 0, Assembler::zero, Assembler::pt, run_compiled_code);
136
    __ ld_ptr(G5_method, in_bytes(Method::interpreter_entry_offset()), target);
137 138 139 140 141
    __ jmp(target, 0);
    __ delayed()->nop();
    __ BIND(run_compiled_code);
    // Note: we could fill some delay slots here, but
    // it doesn't matter, since this is interpreter code.
142
  }
143

144 145
  const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
                                                     Method::from_interpreted_offset();
146
  __ ld_ptr(G5_method, in_bytes(entry_offset), target);
147 148 149 150
  __ jmp(target, 0);
  __ delayed()->nop();
}

151 152 153 154 155 156 157
void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
                                        Register recv, Register method_temp,
                                        Register temp2, Register temp3,
                                        bool for_compiler_entry) {
  BLOCK_COMMENT("jump_to_lambda_form {");
  // This is the initial entry point of a lazy method handle.
  // After type checking, it picks up the invoker from the LambdaForm.
158
  assert_different_registers(recv, method_temp, temp2);  // temp3 is only passed on
159 160 161 162 163 164
  assert(method_temp == G5_method, "required register for loading method");

  //NOT_PRODUCT({ FlagSetting fs(TraceMethodHandles, true); trace_method_handle(_masm, "LZMH"); });

  // Load the invoker, as MH -> MH.form -> LF.vmentry
  __ verify_oop(recv);
165
  __ load_heap_oop(Address(recv,        NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes())),   method_temp);
166
  __ verify_oop(method_temp);
167
  __ load_heap_oop(Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes())),  method_temp);
168
  __ verify_oop(method_temp);
169
  // the following assumes that a Method* is normally compressed in the vmtarget field:
170
  __ ld_ptr(       Address(method_temp, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes())), method_temp);
171 172 173

  if (VerifyMethodHandles && !for_compiler_entry) {
    // make sure recv is already on stack
174
    __ load_sized_value(Address(method_temp, Method::size_of_parameters_offset()),
175 176
                        temp2,
                        sizeof(u2), /*is_signed*/ false);
177
    // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
178 179 180 181 182 183 184 185 186 187 188
    Label L;
    __ ld_ptr(__ argument_address(temp2, temp2, -1), temp2);
    __ cmp_and_br_short(temp2, recv, Assembler::equal, Assembler::pt, L);
    __ STOP("receiver not on stack");
    __ BIND(L);
  }

  jump_from_method_handle(_masm, method_temp, temp2, temp3, for_compiler_entry);
  BLOCK_COMMENT("} jump_to_lambda_form");
}

189

190
// Code generation
191 192 193 194 195 196 197 198 199 200 201 202 203 204
address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
                                                                vmIntrinsics::ID iid) {
  const bool not_for_compiler_entry = false;  // this is the interpreter entry
  assert(is_signature_polymorphic(iid), "expected invoke iid");
  if (iid == vmIntrinsics::_invokeGeneric ||
      iid == vmIntrinsics::_compiledLambdaForm) {
    // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
    // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
    // They all allow an appendix argument.
    __ should_not_reach_here();           // empty stubs make SG sick
    return NULL;
  }

  // I5_savedSP/O5_savedSP: sender SP (must preserve; see prepare_to_jump_from_interpreted)
205
  // G5_method:  Method*
T
twisti 已提交
206
  // G4 (Gargs): incoming argument list (must preserve)
207 208 209 210 211
  // O0: used as temp to hold mh or receiver
  // O1, O4: garbage temps, blown away
  Register O1_scratch    = O1;
  Register O4_param_size = O4;   // size of parameters

T
twisti 已提交
212 213 214 215
  // here's where control starts out:
  __ align(CodeEntryAlignment);
  address entry_point = __ pc();

216 217
  if (VerifyMethodHandles) {
    Label L;
218
    BLOCK_COMMENT("verify_intrinsic_id {");
219
    __ ldub(Address(G5_method, Method::intrinsic_id_offset_in_bytes()), O1_scratch);
220 221 222 223
    __ cmp_and_br_short(O1_scratch, (int) iid, Assembler::equal, Assembler::pt, L);
    if (iid == vmIntrinsics::_linkToVirtual ||
        iid == vmIntrinsics::_linkToSpecial) {
      // 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 232 233 234 235
  // First task:  Find out how big the argument list is.
  Address O4_first_arg_addr;
  int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
  assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
  if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
236
    __ load_sized_value(Address(G5_method, Method::size_of_parameters_offset()),
237 238
                        O4_param_size,
                        sizeof(u2), /*is_signed*/ false);
239
    // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
240 241 242 243
    O4_first_arg_addr = __ argument_address(O4_param_size, O4_param_size, -1);
  } else {
    DEBUG_ONLY(O4_param_size = noreg);
  }
T
twisti 已提交
244

245 246 247 248 249
  Register O0_mh = noreg;
  if (!is_signature_polymorphic_static(iid)) {
    __ ld_ptr(O4_first_arg_addr, O0_mh = O0);
    DEBUG_ONLY(O4_param_size = noreg);
  }
T
twisti 已提交
250

251 252 253 254 255
  // O4_first_arg_addr is live!

  if (TraceMethodHandles) {
    if (O0_mh != noreg)
      __ mov(O0_mh, G3_method_handle);  // make stub happy
256
    trace_method_handle_interpreter_entry(_masm, iid);
257
  }
258

259 260
  if (iid == vmIntrinsics::_invokeBasic) {
    generate_method_handle_dispatch(_masm, iid, O0_mh, noreg, not_for_compiler_entry);
T
twisti 已提交
261

262
  } else {
263 264 265 266 267 268
    // Adjust argument list by popping the trailing MemberName argument.
    Register O0_recv = noreg;
    if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
      // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
      __ ld_ptr(O4_first_arg_addr, O0_recv = O0);
      DEBUG_ONLY(O4_param_size = noreg);
269
    }
270 271 272 273
    Register G5_member = G5_method;  // MemberName ptr; incoming method ptr is dead now
    __ ld_ptr(__ argument_address(constant(0)), G5_member);
    __ add(Gargs, Interpreter::stackElementSize, Gargs);
    generate_method_handle_dispatch(_masm, iid, O0_recv, G5_member, not_for_compiler_entry);
274
  }
T
twisti 已提交
275

276
  return entry_point;
277
}
T
twisti 已提交
278

279 280 281 282 283 284 285
void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
                                                    vmIntrinsics::ID iid,
                                                    Register receiver_reg,
                                                    Register member_reg,
                                                    bool for_compiler_entry) {
  assert(is_signature_polymorphic(iid), "expected invoke iid");
  Register temp1 = (for_compiler_entry ? G1_scratch : O1);
286 287 288
  Register temp2 = (for_compiler_entry ? G3_scratch : O2);
  Register temp3 = (for_compiler_entry ? G4_scratch : O3);
  Register temp4 = (for_compiler_entry ? noreg      : O4);
289 290
  if (for_compiler_entry) {
    assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : O0), "only valid assignment");
291 292 293 294 295 296
    assert_different_registers(temp1, O0, O1, O2, O3, O4, O5);
    assert_different_registers(temp2, O0, O1, O2, O3, O4, O5);
    assert_different_registers(temp3, O0, O1, O2, O3, O4, O5);
    assert_different_registers(temp4, O0, O1, O2, O3, O4, O5);
  } else {
    assert_different_registers(temp1, temp2, temp3, temp4, O5_savedSP);  // don't trash lastSP
T
twisti 已提交
297
  }
298 299
  if (receiver_reg != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
  if (member_reg   != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
T
twisti 已提交
300

301 302
  if (iid == vmIntrinsics::_invokeBasic) {
    // indirect through MH.form.vmentry.vmtarget
303
    jump_to_lambda_form(_masm, receiver_reg, G5_method, temp1, temp2, for_compiler_entry);
T
twisti 已提交
304

305 306 307 308
  } else {
    // The method is a member invoker used by direct method handles.
    if (VerifyMethodHandles) {
      // 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 311 312
                   temp1, temp2,
                   "MemberName required for invokeVirtual etc.");
    }
T
twisti 已提交
313

314 315 316
    Address member_clazz(    member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
    Address member_vmindex(  member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
    Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes()));
T
twisti 已提交
317

318 319 320 321 322 323 324 325 326 327
    Register temp1_recv_klass = temp1;
    if (iid != vmIntrinsics::_linkToStatic) {
      __ verify_oop(receiver_reg);
      if (iid == vmIntrinsics::_linkToSpecial) {
        // Don't actually load the klass; just null-check the receiver.
        __ null_check(receiver_reg);
      } else {
        // load receiver klass itself
        __ null_check(receiver_reg, oopDesc::klass_offset_in_bytes());
        __ load_klass(receiver_reg, temp1_recv_klass);
328
        __ verify_klass_ptr(temp1_recv_klass);
329 330 331 332 333 334 335
      }
      BLOCK_COMMENT("check_receiver {");
      // The receiver for the MemberName must be in receiver_reg.
      // Check the receiver against the MemberName.clazz
      if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
        // Did not load it above...
        __ load_klass(receiver_reg, temp1_recv_klass);
336
        __ verify_klass_ptr(temp1_recv_klass);
337 338 339 340 341 342
      }
      if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
        Label L_ok;
        Register temp2_defc = temp2;
        __ load_heap_oop(member_clazz, temp2_defc);
        load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
343
        __ verify_klass_ptr(temp2_defc);
344 345 346 347 348 349 350 351 352 353 354
        __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
        // If we get here, the type check failed!
        __ STOP("receiver class disagrees with MemberName.clazz");
        __ bind(L_ok);
      }
      BLOCK_COMMENT("} check_receiver");
    }
    if (iid == vmIntrinsics::_linkToSpecial ||
        iid == vmIntrinsics::_linkToStatic) {
      DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
    }
T
twisti 已提交
355

356 357 358 359
    // Live registers at this point:
    //  member_reg - MemberName that was the trailing argument
    //  temp1_recv_klass - klass of stacked receiver, if needed
    //  O5_savedSP - interpreter linkage (if interpreted)
360
    //  O0..O5 - compiler arguments (if compiled)
T
twisti 已提交
361

362
    Label L_incompatible_class_change_error;
363 364 365
    switch (iid) {
    case vmIntrinsics::_linkToSpecial:
      if (VerifyMethodHandles) {
366
        verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp2);
367
      }
368
      __ ld_ptr(member_vmtarget, G5_method);
369
      break;
370

371 372
    case vmIntrinsics::_linkToStatic:
      if (VerifyMethodHandles) {
373
        verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp2);
374
      }
375
      __ ld_ptr(member_vmtarget, G5_method);
376
      break;
377

378 379 380 381 382 383
    case vmIntrinsics::_linkToVirtual:
    {
      // same as TemplateTable::invokevirtual,
      // minus the CP setup and profiling:

      if (VerifyMethodHandles) {
384
        verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp2);
385 386 387 388 389 390 391 392 393 394 395 396
      }

      // pick out the vtable index from the MemberName, and then we can discard it:
      Register temp2_index = temp2;
      __ ld_ptr(member_vmindex, temp2_index);

      if (VerifyMethodHandles) {
        Label L_index_ok;
        __ cmp_and_br_short(temp2_index, (int) 0, Assembler::greaterEqual, Assembler::pn, L_index_ok);
        __ STOP("no virtual index");
        __ BIND(L_index_ok);
      }
397

398 399
      // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
      // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
400

401
      // get target Method* & entry point
402 403
      __ lookup_virtual_method(temp1_recv_klass, temp2_index, G5_method);
      break;
404 405
    }

406 407 408 409 410
    case vmIntrinsics::_linkToInterface:
    {
      // same as TemplateTable::invokeinterface
      // (minus the CP setup and profiling, with different argument motion)
      if (VerifyMethodHandles) {
411
        verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp2);
412 413
      }

414 415 416 417
      Register temp2_intf = temp2;
      __ load_heap_oop(member_clazz, temp2_intf);
      load_klass_from_Class(_masm, temp2_intf, temp3, temp4);
      __ verify_klass_ptr(temp2_intf);
418 419 420 421 422 423 424 425 426 427 428

      Register G5_index = G5_method;
      __ ld_ptr(member_vmindex, G5_index);
      if (VerifyMethodHandles) {
        Label L;
        __ cmp_and_br_short(G5_index, 0, Assembler::greaterEqual, Assembler::pt, L);
        __ STOP("invalid vtable index for MH.invokeInterface");
        __ bind(L);
      }

      // given intf, index, and recv klass, dispatch to the implementation method
429
      __ lookup_interface_method(temp1_recv_klass, temp2_intf,
430 431
                                 // note: next two args must be the same:
                                 G5_index, G5_method,
432 433
                                 temp3, temp4,
                                 L_incompatible_class_change_error);
434
      break;
435 436
    }

437
    default:
438
      fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
439
      break;
440 441
    }

442 443 444 445 446 447 448 449 450
    // Live at this point:
    //   G5_method
    //   O5_savedSP (if interpreted)

    // After figuring out which concrete method to call, jump into it.
    // Note that this works in the interpreter with no data motion.
    // But the compiled version will require that rcx_recv be shifted out.
    __ verify_method_ptr(G5_method);
    jump_from_method_handle(_masm, G5_method, temp1, temp2, for_compiler_entry);
451

452 453 454 455 456
    if (iid == vmIntrinsics::_linkToInterface) {
      __ BIND(L_incompatible_class_change_error);
      AddressLiteral icce(StubRoutines::throw_IncompatibleClassChangeError_entry());
      __ jump_to(icce, temp1);
      __ delayed()->nop();
457 458 459
    }
  }
}
T
twisti 已提交
460

461
#ifndef PRODUCT
T
twisti 已提交
462
void trace_method_handle_stub(const char* adaptername,
463
                              oopDesc* mh,
464 465 466
                              intptr_t* saved_sp,
                              intptr_t* args,
                              intptr_t* tracing_fp) {
467 468 469 470 471 472
  bool has_mh = (strstr(adaptername, "/static") == NULL &&
                 strstr(adaptername, "linkTo") == NULL);    // static linkers don't have MH
  const char* mh_reg_name = has_mh ? "G3_mh" : "G3";
  tty->print_cr("MH %s %s="INTPTR_FORMAT " saved_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT,
                adaptername, mh_reg_name,
                (intptr_t) mh, saved_sp, args);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

  if (Verbose) {
    // dumping last frame with frame::describe

    JavaThread* p = JavaThread::active();

    ResourceMark rm;
    PRESERVE_EXCEPTION_MARK; // may not be needed by safer and unexpensive here
    FrameValues values;

    // Note: We want to allow trace_method_handle from any call site.
    // While trace_method_handle creates a frame, it may be entered
    // without a valid return PC in O7 (e.g. not just after a call).
    // Walking that frame could lead to failures due to that invalid PC.
    // => carefully detect that frame when doing the stack walking

    // walk up to the right frame using the "tracing_fp" argument
    intptr_t* cur_sp = StubRoutines::Sparc::flush_callers_register_windows_func()();
    frame cur_frame(cur_sp, frame::unpatchable, NULL);

    while (cur_frame.fp() != (intptr_t *)(STACK_BIAS+(uintptr_t)tracing_fp)) {
      cur_frame = os::get_sender_for_C_frame(&cur_frame);
    }

    // safely create a frame and call frame::describe
    intptr_t *dump_sp = cur_frame.sender_sp();
    intptr_t *dump_fp = cur_frame.link();

    bool walkable = has_mh; // whether the traced frame shoud be walkable

    // the sender for cur_frame is the caller of trace_method_handle
    if (walkable) {
      // The previous definition of walkable may have to be refined
      // if new call sites cause the next frame constructor to start
      // failing. Alternatively, frame constructors could be
      // modified to support the current or future non walkable
      // frames (but this is more intrusive and is not considered as
      // part of this RFE, which will instead use a simpler output).
      frame dump_frame = frame(dump_sp,
                               cur_frame.sp(), // younger_sp
                               false); // no adaptation
      dump_frame.describe(values, 1);
    } else {
      // Robust dump for frames which cannot be constructed from sp/younger_sp
      // Add descriptions without building a Java frame to avoid issues
      values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
      values.describe(-1, dump_sp, "sp");
    }

    bool has_args = has_mh; // whether Gargs is meaningful

    // mark args, if seems valid (may not be valid for some adapters)
    if (has_args) {
      if ((args >= dump_sp) && (args < dump_fp)) {
        values.describe(-1, args, "*G4_args");
      }
    }

    // mark saved_sp, if seems valid (may not be valid for some adapters)
    intptr_t *unbiased_sp = (intptr_t *)(STACK_BIAS+(uintptr_t)saved_sp);
533
    const int ARG_LIMIT = 255, SLOP = 45, UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP);
534 535 536 537 538 539 540
    if ((unbiased_sp >= dump_sp - UNREASONABLE_STACK_MOVE) && (unbiased_sp < dump_fp)) {
      values.describe(-1, unbiased_sp, "*saved_sp+STACK_BIAS");
    }

    // Note: the unextended_sp may not be correct
    tty->print_cr("  stack layout:");
    values.print(p);
541 542 543 544 545 546 547
    if (has_mh && mh->is_oop()) {
      mh->print();
      if (java_lang_invoke_MethodHandle::is_instance(mh)) {
        if (java_lang_invoke_MethodHandle::form_offset_in_bytes() != 0)
          java_lang_invoke_MethodHandle::form(mh)->print();
      }
    }
548
  }
T
twisti 已提交
549
}
550

551 552 553 554
void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
  if (!TraceMethodHandles)  return;
  BLOCK_COMMENT("trace_method_handle {");
  // save: Gargs, O5_savedSP
555 556
  __ save_frame(16); // need space for saving required FPU state

557 558
  __ set((intptr_t) adaptername, O0);
  __ mov(G3_method_handle, O1);
559
  __ mov(I5_savedSP, O2);
560 561 562 563 564 565 566 567 568 569 570
  __ mov(Gargs, O3);
  __ mov(I6, O4); // frame identifier for safe stack walking

  // Save scratched registers that might be needed. Robustness is more
  // important than optimizing the saves for this debug only code.

  // save FP result, valid at some call sites (adapter_opt_return_float, ...)
  Address d_save(FP, -sizeof(jdouble) + STACK_BIAS);
  __ stf(FloatRegisterImpl::D, Ftos_d, d_save);
  // Safely save all globals but G2 (handled by call_VM_leaf) and G7
  // (OS reserved).
571 572 573
  __ mov(G3_method_handle, L3);
  __ mov(Gargs, L4);
  __ mov(G5_method_type, L5);
574 575 576 577
  __ mov(G6, L6);
  __ mov(G1, L1);

  __ call_VM_leaf(L2 /* for G2 */, CAST_FROM_FN_PTR(address, trace_method_handle_stub));
578 579 580 581

  __ mov(L3, G3_method_handle);
  __ mov(L4, Gargs);
  __ mov(L5, G5_method_type);
582 583 584 585
  __ mov(L6, G6);
  __ mov(L1, G1);
  __ ldf(FloatRegisterImpl::D, d_save, Ftos_d);

586 587 588
  __ restore();
  BLOCK_COMMENT("} trace_method_handle");
}
T
twisti 已提交
589
#endif // PRODUCT