methodHandles_sparc.cpp 97.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, 2011, 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 26 27 28
#include "precompiled.hpp"
#include "interpreter/interpreter.hpp"
#include "memory/allocation.inline.hpp"
#include "prims/methodHandles.hpp"
29 30 31

#define __ _masm->

32 33 34 35 36 37 38 39
#ifdef PRODUCT
#define BLOCK_COMMENT(str) /* nothing */
#else
#define BLOCK_COMMENT(str) __ block_comment(str)
#endif

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

40 41
address MethodHandleEntry::start_compiled_entry(MacroAssembler* _masm,
                                                address interpreted_entry) {
T
twisti 已提交
42 43 44
  // Just before the actual machine code entry point, allocate space
  // for a MethodHandleEntry::Data record, so that we can manage everything
  // from one base pointer.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  __ align(wordSize);
  address target = __ pc() + sizeof(Data);
  while (__ pc() < target) {
    __ nop();
    __ align(wordSize);
  }

  MethodHandleEntry* me = (MethodHandleEntry*) __ pc();
  me->set_end_address(__ pc());         // set a temporary end_address
  me->set_from_interpreted_entry(interpreted_entry);
  me->set_type_checking_entry(NULL);

  return (address) me;
}

MethodHandleEntry* MethodHandleEntry::finish_compiled_entry(MacroAssembler* _masm,
                                                address start_addr) {
  MethodHandleEntry* me = (MethodHandleEntry*) start_addr;
  assert(me->end_address() == start_addr, "valid ME");

  // Fill in the real end_address:
  __ align(wordSize);
  me->set_end_address(__ pc());

  return me;
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
// stack walking support

frame MethodHandles::ricochet_frame_sender(const frame& fr, RegisterMap *map) {
  //RicochetFrame* f = RicochetFrame::from_frame(fr);
  // Cf. is_interpreted_frame path of frame::sender
  intptr_t* younger_sp = fr.sp();
  intptr_t* sp         = fr.sender_sp();
  map->make_integer_regs_unsaved();
  map->shift_window(sp, younger_sp);
  bool this_frame_adjusted_stack = true;  // I5_savedSP is live in this RF
  return frame(sp, younger_sp, this_frame_adjusted_stack);
}

void MethodHandles::ricochet_frame_oops_do(const frame& fr, OopClosure* blk, const RegisterMap* reg_map) {
  ResourceMark rm;
  RicochetFrame* f = RicochetFrame::from_frame(fr);

  // pick up the argument type descriptor:
  Thread* thread = Thread::current();
  Handle cookie(thread, f->compute_saved_args_layout(true, true));

  // process fixed part
  blk->do_oop((oop*)f->saved_target_addr());
  blk->do_oop((oop*)f->saved_args_layout_addr());

  // process variable arguments:
  if (cookie.is_null())  return;  // no arguments to describe

  // the cookie is actually the invokeExact method for my target
  // his argument signature is what I'm interested in
  assert(cookie->is_method(), "");
  methodHandle invoker(thread, methodOop(cookie()));
  assert(invoker->name() == vmSymbols::invokeExact_name(), "must be this kind of method");
  assert(!invoker->is_static(), "must have MH argument");
  int slot_count = invoker->size_of_parameters();
  assert(slot_count >= 1, "must include 'this'");
  intptr_t* base = f->saved_args_base();
  intptr_t* retval = NULL;
  if (f->has_return_value_slot())
    retval = f->return_value_slot_addr();
  int slot_num = slot_count - 1;
  intptr_t* loc = &base[slot_num];
  //blk->do_oop((oop*) loc);   // original target, which is irrelevant
  int arg_num = 0;
  for (SignatureStream ss(invoker->signature()); !ss.is_done(); ss.next()) {
    if (ss.at_return_type())  continue;
    BasicType ptype = ss.type();
    if (ptype == T_ARRAY)  ptype = T_OBJECT; // fold all refs to T_OBJECT
    assert(ptype >= T_BOOLEAN && ptype <= T_OBJECT, "not array or void");
    slot_num -= type2size[ptype];
    loc = &base[slot_num];
    bool is_oop = (ptype == T_OBJECT && loc != retval);
    if (is_oop)  blk->do_oop((oop*)loc);
    arg_num += 1;
  }
  assert(slot_num == 0, "must have processed all the arguments");
}

// Ricochet Frames
const Register MethodHandles::RicochetFrame::L1_continuation      = L1;
const Register MethodHandles::RicochetFrame::L2_saved_target      = L2;
const Register MethodHandles::RicochetFrame::L3_saved_args_layout = L3;
const Register MethodHandles::RicochetFrame::L4_saved_args_base   = L4; // cf. Gargs = G4
const Register MethodHandles::RicochetFrame::L5_conversion        = L5;
#ifdef ASSERT
const Register MethodHandles::RicochetFrame::L0_magic_number_1    = L0;
#endif //ASSERT

oop MethodHandles::RicochetFrame::compute_saved_args_layout(bool read_cache, bool write_cache) {
  if (read_cache) {
    oop cookie = saved_args_layout();
    if (cookie != NULL)  return cookie;
  }
  oop target = saved_target();
  oop mtype  = java_lang_invoke_MethodHandle::type(target);
  oop mtform = java_lang_invoke_MethodType::form(mtype);
  oop cookie = java_lang_invoke_MethodTypeForm::vmlayout(mtform);
  if (write_cache)  {
    (*saved_args_layout_addr()) = cookie;
  }
  return cookie;
}

void MethodHandles::RicochetFrame::generate_ricochet_blob(MacroAssembler* _masm,
                                                          // output params:
                                                          int* bounce_offset,
                                                          int* exception_offset,
                                                          int* frame_size_in_words) {
  (*frame_size_in_words) = RicochetFrame::frame_size_in_bytes() / wordSize;

  address start = __ pc();

#ifdef ASSERT
  __ illtrap(0); __ illtrap(0); __ illtrap(0);
  // here's a hint of something special:
  __ set(MAGIC_NUMBER_1, G0);
  __ set(MAGIC_NUMBER_2, G0);
#endif //ASSERT
  __ illtrap(0);  // not reached

  // Return values are in registers.
  // L1_continuation contains a cleanup continuation we must return
  // to.

  (*bounce_offset) = __ pc() - start;
  BLOCK_COMMENT("ricochet_blob.bounce");

  if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);
  trace_method_handle(_masm, "ricochet_blob.bounce");

  __ JMP(L1_continuation, 0);
  __ delayed()->nop();
  __ illtrap(0);

  DEBUG_ONLY(__ set(MAGIC_NUMBER_2, G0));

  (*exception_offset) = __ pc() - start;
  BLOCK_COMMENT("ricochet_blob.exception");

  // compare this to Interpreter::rethrow_exception_entry, which is parallel code
  // for example, see TemplateInterpreterGenerator::generate_throw_exception
  // Live registers in:
  //   Oexception  (O0): exception
  //   Oissuing_pc (O1): return address/pc that threw exception (ignored, always equal to bounce addr)
  __ verify_oop(Oexception);

  // Take down the frame.

  // Cf. InterpreterMacroAssembler::remove_activation.
  leave_ricochet_frame(_masm, /*recv_reg=*/ noreg, I5_savedSP, I7);

  // We are done with this activation frame; find out where to go next.
  // The continuation point will be an exception handler, which expects
  // the following registers set up:
  //
  // Oexception: exception
  // Oissuing_pc: the local call that threw exception
  // Other On: garbage
  // In/Ln:  the contents of the caller's register window
  //
  // We do the required restore at the last possible moment, because we
  // need to preserve some state across a runtime call.
  // (Remember that the caller activation is unknown--it might not be
  // interpreted, so things like Lscratch are useless in the caller.)
  __ mov(Oexception,  Oexception ->after_save());  // get exception in I0 so it will be on O0 after restore
  __ add(I7, frame::pc_return_offset, Oissuing_pc->after_save());  // likewise set I1 to a value local to the caller
  __ call_VM_leaf(L7_thread_cache,
                  CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
                  G2_thread, Oissuing_pc->after_save());

  // The caller's SP was adjusted upon method entry to accomodate
  // the callee's non-argument locals. Undo that adjustment.
  __ JMP(O0, 0);                         // return exception handler in caller
  __ delayed()->restore(I5_savedSP, G0, SP);

  // (same old exception object is already in Oexception; see above)
  // Note that an "issuing PC" is actually the next PC after the call
}

void MethodHandles::RicochetFrame::enter_ricochet_frame(MacroAssembler* _masm,
                                                        Register recv_reg,
                                                        Register argv_reg,
                                                        address return_handler) {
  // does not include the __ save()
  assert(argv_reg == Gargs, "");
  Address G3_mh_vmtarget(   recv_reg, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
  Address G3_amh_conversion(recv_reg, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());

  // Create the RicochetFrame.
  // Unlike on x86 we can store all required information in local
  // registers.
  BLOCK_COMMENT("push RicochetFrame {");
  __ set(ExternalAddress(return_handler),          L1_continuation);
  __ load_heap_oop(G3_mh_vmtarget,                 L2_saved_target);
  __ mov(G0,                                       L3_saved_args_layout);
  __ mov(Gargs,                                    L4_saved_args_base);
  __ lduw(G3_amh_conversion,                       L5_conversion);  // 32-bit field
  // I5, I6, I7 are already set up
  DEBUG_ONLY(__ set((int32_t) MAGIC_NUMBER_1,      L0_magic_number_1));
  BLOCK_COMMENT("} RicochetFrame");
}

void MethodHandles::RicochetFrame::leave_ricochet_frame(MacroAssembler* _masm,
                                                        Register recv_reg,
                                                        Register new_sp_reg,
                                                        Register sender_pc_reg) {
  assert(new_sp_reg == I5_savedSP, "exact_sender_sp already in place");
  assert(sender_pc_reg == I7, "in a fixed place");
  // does not include the __ ret() & __ restore()
  assert_different_registers(recv_reg, new_sp_reg, sender_pc_reg);
  // Take down the frame.
  // Cf. InterpreterMacroAssembler::remove_activation.
  BLOCK_COMMENT("end_ricochet_frame {");
  if (recv_reg->is_valid())
    __ mov(L2_saved_target, recv_reg);
  BLOCK_COMMENT("} end_ricochet_frame");
}

// Emit code to verify that FP is pointing at a valid ricochet frame.
#ifdef ASSERT
enum {
273
  ARG_LIMIT = 255, SLOP = 45,
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  // use this parameter for checking for garbage stack movements:
  UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
  // the slop defends against false alarms due to fencepost errors
};

void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
  // The stack should look like this:
  //    ... keep1 | dest=42 | keep2 | magic | handler | magic | recursive args | [RF]
  // Check various invariants.

  Register O7_temp = O7, O5_temp = O5;

  Label L_ok_1, L_ok_2, L_ok_3, L_ok_4;
  BLOCK_COMMENT("verify_clean {");
  // Magic numbers must check out:
  __ set((int32_t) MAGIC_NUMBER_1, O7_temp);
  __ cmp(O7_temp, L0_magic_number_1);
  __ br(Assembler::equal, false, Assembler::pt, L_ok_1);
  __ delayed()->nop();
  __ stop("damaged ricochet frame: MAGIC_NUMBER_1 not found");

  __ BIND(L_ok_1);

  // Arguments pointer must look reasonable:
#ifdef _LP64
  Register FP_temp = O5_temp;
  __ add(FP, STACK_BIAS, FP_temp);
#else
  Register FP_temp = FP;
#endif
  __ cmp(L4_saved_args_base, FP_temp);
  __ br(Assembler::greaterEqualUnsigned, false, Assembler::pt, L_ok_2);
  __ delayed()->nop();
  __ stop("damaged ricochet frame: L4 < FP");

  __ BIND(L_ok_2);
310 311 312 313 314 315
  // Disable until we decide on it's fate
  // __ sub(L4_saved_args_base, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize, O7_temp);
  // __ cmp(O7_temp, FP_temp);
  // __ br(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok_3);
  // __ delayed()->nop();
  // __ stop("damaged ricochet frame: (L4 - UNREASONABLE_STACK_MOVE) > FP");
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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

  __ BIND(L_ok_3);
  extract_conversion_dest_type(_masm, L5_conversion, O7_temp);
  __ cmp(O7_temp, T_VOID);
  __ br(Assembler::equal, false, Assembler::pt, L_ok_4);
  __ delayed()->nop();
  extract_conversion_vminfo(_masm, L5_conversion, O5_temp);
  __ ld_ptr(L4_saved_args_base, __ argument_offset(O5_temp, O5_temp), O7_temp);
  assert(__ is_simm13(RETURN_VALUE_PLACEHOLDER), "must be simm13");
  __ cmp(O7_temp, (int32_t) RETURN_VALUE_PLACEHOLDER);
  __ brx(Assembler::equal, false, Assembler::pt, L_ok_4);
  __ delayed()->nop();
  __ stop("damaged ricochet frame: RETURN_VALUE_PLACEHOLDER not found");
  __ BIND(L_ok_4);
  BLOCK_COMMENT("} verify_clean");
}
#endif //ASSERT

void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
  if (VerifyMethodHandles)
    verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(), temp_reg, temp2_reg,
                 "AMH argument is a Class");
  __ load_heap_oop(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
}

void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Address conversion_field_addr, Register reg) {
  assert(CONV_VMINFO_SHIFT == 0, "preshifted");
  assert(CONV_VMINFO_MASK == right_n_bits(BitsPerByte), "else change type of following load");
  __ ldub(conversion_field_addr.plus_disp(BytesPerInt - 1), reg);
}

void MethodHandles::extract_conversion_vminfo(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
  assert(CONV_VMINFO_SHIFT == 0, "preshifted");
  __ and3(conversion_field_reg, CONV_VMINFO_MASK, reg);
}

void MethodHandles::extract_conversion_dest_type(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
  __ srl(conversion_field_reg, CONV_DEST_TYPE_SHIFT, reg);
  __ and3(reg, 0x0F, reg);
}

void MethodHandles::load_stack_move(MacroAssembler* _masm,
                                    Address G3_amh_conversion,
                                    Register stack_move_reg) {
  BLOCK_COMMENT("load_stack_move {");
  __ ldsw(G3_amh_conversion, stack_move_reg);
  __ sra(stack_move_reg, CONV_STACK_MOVE_SHIFT, stack_move_reg);
  if (VerifyMethodHandles) {
    Label L_ok, L_bad;
    int32_t stack_move_limit = 0x0800;  // extra-large
    __ cmp(stack_move_reg, stack_move_limit);
    __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
    __ delayed()->nop();
    __ cmp(stack_move_reg, -stack_move_limit);
    __ br(Assembler::greater, false, Assembler::pt, L_ok);
    __ delayed()->nop();
    __ BIND(L_bad);
    __ stop("load_stack_move of garbage value");
    __ BIND(L_ok);
  }
  BLOCK_COMMENT("} load_stack_move");
}

#ifdef ASSERT
void MethodHandles::RicochetFrame::verify() const {
  assert(magic_number_1() == MAGIC_NUMBER_1, "");
  if (!Universe::heap()->is_gc_active()) {
    if (saved_args_layout() != NULL) {
      assert(saved_args_layout()->is_method(), "must be valid oop");
    }
    if (saved_target() != NULL) {
      assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
    }
  }
  int conv_op = adapter_conversion_op(conversion());
  assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
         conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
         conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
         "must be a sane conversion");
  if (has_return_value_slot()) {
    assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
  }
}

void MethodHandles::verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
  // Verify that argslot lies within (Gargs, FP].
  Label L_ok, L_bad;
  BLOCK_COMMENT("verify_argslot {");
  __ add(FP, STACK_BIAS, temp_reg);  // STACK_BIAS is zero on !_LP64
  __ cmp(argslot_reg, temp_reg);
  __ brx(Assembler::greaterUnsigned, false, Assembler::pn, L_bad);
  __ delayed()->nop();
  __ cmp(Gargs, argslot_reg);
  __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
  __ delayed()->nop();
  __ BIND(L_bad);
  __ stop(error_message);
  __ BIND(L_ok);
  BLOCK_COMMENT("} verify_argslot");
}

void MethodHandles::verify_argslots(MacroAssembler* _masm,
                                    RegisterOrConstant arg_slots,
                                    Register arg_slot_base_reg,
                                    Register temp_reg,
                                    Register temp2_reg,
                                    bool negate_argslots,
                                    const char* error_message) {
  // Verify that [argslot..argslot+size) lies within (Gargs, FP).
  Label L_ok, L_bad;
  BLOCK_COMMENT("verify_argslots {");
  if (negate_argslots) {
    if (arg_slots.is_constant()) {
      arg_slots = -1 * arg_slots.as_constant();
    } else {
      __ neg(arg_slots.as_register(), temp_reg);
      arg_slots = temp_reg;
    }
  }
  __ add(arg_slot_base_reg, __ argument_offset(arg_slots, temp_reg), temp_reg);
  __ add(FP, STACK_BIAS, temp2_reg);  // STACK_BIAS is zero on !_LP64
  __ cmp(temp_reg, temp2_reg);
  __ brx(Assembler::greaterUnsigned, false, Assembler::pn, L_bad);
  __ delayed()->nop();
  // Gargs points to the first word so adjust by BytesPerWord
  __ add(arg_slot_base_reg, BytesPerWord, temp_reg);
  __ cmp(Gargs, temp_reg);
  __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
  __ delayed()->nop();
  __ BIND(L_bad);
  __ stop(error_message);
  __ BIND(L_ok);
  BLOCK_COMMENT("} verify_argslots");
}

// Make sure that arg_slots has the same sign as the given direction.
// If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
void MethodHandles::verify_stack_move(MacroAssembler* _masm,
                                      RegisterOrConstant arg_slots, int direction) {
  enum { UNREASONABLE_STACK_MOVE = 256 * 4 };  // limit of 255 arguments
  bool allow_zero = arg_slots.is_constant();
  if (direction == 0) { direction = +1; allow_zero = true; }
  assert(stack_move_unit() == -1, "else add extra checks here");
  if (arg_slots.is_register()) {
    Label L_ok, L_bad;
    BLOCK_COMMENT("verify_stack_move {");
    // __ btst(-stack_move_unit() - 1, arg_slots.as_register());  // no need
    // __ br(Assembler::notZero, false, Assembler::pn, L_bad);
    // __ delayed()->nop();
    __ cmp(arg_slots.as_register(), (int32_t) NULL_WORD);
    if (direction > 0) {
      __ br(allow_zero ? Assembler::less : Assembler::lessEqual, false, Assembler::pn, L_bad);
      __ delayed()->nop();
      __ cmp(arg_slots.as_register(), (int32_t) UNREASONABLE_STACK_MOVE);
      __ br(Assembler::less, false, Assembler::pn, L_ok);
      __ delayed()->nop();
    } else {
      __ br(allow_zero ? Assembler::greater : Assembler::greaterEqual, false, Assembler::pn, L_bad);
      __ delayed()->nop();
      __ cmp(arg_slots.as_register(), (int32_t) -UNREASONABLE_STACK_MOVE);
      __ br(Assembler::greater, false, Assembler::pn, L_ok);
      __ delayed()->nop();
    }
    __ BIND(L_bad);
    if (direction > 0)
      __ stop("assert arg_slots > 0");
    else
      __ stop("assert arg_slots < 0");
    __ BIND(L_ok);
    BLOCK_COMMENT("} verify_stack_move");
  } else {
    intptr_t size = arg_slots.as_constant();
    if (direction < 0)  size = -size;
    assert(size >= 0, "correct direction of constant move");
    assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
  }
}

void MethodHandles::verify_klass(MacroAssembler* _masm,
                                 Register obj_reg, KlassHandle klass,
                                 Register temp_reg, Register temp2_reg,
                                 const char* error_message) {
  oop* klass_addr = klass.raw_value();
  assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
         klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
         "must be one of the SystemDictionaryHandles");
  Label L_ok, L_bad;
  BLOCK_COMMENT("verify_klass {");
  __ verify_oop(obj_reg);
  __ br_null(obj_reg, false, Assembler::pn, L_bad);
  __ delayed()->nop();
  __ load_klass(obj_reg, temp_reg);
  __ set(ExternalAddress(klass_addr), temp2_reg);
  __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
  __ cmp(temp_reg, temp2_reg);
  __ brx(Assembler::equal, false, Assembler::pt, L_ok);
  __ delayed()->nop();
  intptr_t super_check_offset = klass->super_check_offset();
  __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
  __ set(ExternalAddress(klass_addr), temp2_reg);
  __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
  __ cmp(temp_reg, temp2_reg);
  __ brx(Assembler::equal, false, Assembler::pt, L_ok);
  __ delayed()->nop();
  __ BIND(L_bad);
  __ stop(error_message);
  __ BIND(L_ok);
  BLOCK_COMMENT("} verify_klass");
}
#endif // ASSERT
526

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp) {
  assert(method == G5_method, "interpreter calling convention");
  __ verify_oop(method);
  __ ld_ptr(G5_method, in_bytes(methodOopDesc::from_interpreted_offset()), target);
  if (JvmtiExport::can_post_interpreter_events()) {
    // 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();
    Label skip_compiled_code;

    const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
    __ ld(interp_only, temp);
    __ tst(temp);
    __ br(Assembler::notZero, true, Assembler::pn, skip_compiled_code);
    __ delayed()->ld_ptr(G5_method, in_bytes(methodOopDesc::interpreter_entry_offset()), target);
    __ bind(skip_compiled_code);
  }
  __ jmp(target, 0);
  __ delayed()->nop();
}


551 552
// Code generation
address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
553
  // I5_savedSP/O5_savedSP: sender SP (must preserve)
T
twisti 已提交
554
  // G4 (Gargs): incoming argument list (must preserve)
555
  // G5_method:  invoke methodOop
T
twisti 已提交
556
  // G3_method_handle: receiver method handle (must load from sp[MethodTypeForm.vmslots])
557 558
  // O0, O1, O2, O3, O4: garbage temps, blown away
  Register O0_mtype   = O0;
T
twisti 已提交
559
  Register O1_scratch = O1;
560 561
  Register O2_scratch = O2;
  Register O3_scratch = O3;
562
  Register O4_argslot = O4;
563
  Register O4_argbase = O4;
T
twisti 已提交
564 565 566 567

  // emit WrongMethodType path first, to enable back-branch from main path
  Label wrong_method_type;
  __ bind(wrong_method_type);
568 569 570 571 572 573
  Label invoke_generic_slow_path;
  assert(methodOopDesc::intrinsic_id_size_in_bytes() == sizeof(u1), "");;
  __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
  __ cmp(O1_scratch, (int) vmIntrinsics::_invokeExact);
  __ brx(Assembler::notEqual, false, Assembler::pt, invoke_generic_slow_path);
  __ delayed()->nop();
574
  __ mov(O0_mtype, G5_method_type);  // required by throw_WrongMethodType
575 576 577
  __ mov(G3_method_handle, G3_method_handle);  // already in this register
  // O0 will be filled in with JavaThread in stub
  __ jump_to(AddressLiteral(StubRoutines::throw_WrongMethodTypeException_entry()), O3_scratch);
T
twisti 已提交
578 579 580 581 582 583
  __ delayed()->nop();

  // here's where control starts out:
  __ align(CodeEntryAlignment);
  address entry_point = __ pc();

584
  // fetch the MethodType from the method handle
585 586 587
  // FIXME: Interpreter should transmit pre-popped stack pointer, to locate base of arg list.
  // This would simplify several touchy bits of code.
  // See 6984712: JSR 292 method handle calls need a clean argument base pointer
T
twisti 已提交
588 589 590
  {
    Register tem = G5_method;
    for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
591 592
      __ ld_ptr(Address(tem, *pchase), O0_mtype);
      tem = O0_mtype;          // in case there is another indirection
T
twisti 已提交
593 594 595 596
    }
  }

  // given the MethodType, find out where the MH argument is buried
597 598
  __ load_heap_oop(Address(O0_mtype,   __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes,        O1_scratch)), O4_argslot);
  __ ldsw(         Address(O4_argslot, __ delayed_value(java_lang_invoke_MethodTypeForm::vmslots_offset_in_bytes, O1_scratch)), O4_argslot);
599
  __ add(__ argument_address(O4_argslot, O4_argslot, 1), O4_argbase);
600
  // Note: argument_address uses its input as a scratch register!
601 602
  Address mh_receiver_slot_addr(O4_argbase, -Interpreter::stackElementSize);
  __ ld_ptr(mh_receiver_slot_addr, G3_method_handle);
603 604 605

  trace_method_handle(_masm, "invokeExact");

606
  __ check_method_handle_type(O0_mtype, G3_method_handle, O1_scratch, wrong_method_type);
607 608 609 610

  // Nobody uses the MH receiver slot after this.  Make sure.
  DEBUG_ONLY(__ set((int32_t) 0x999999, O1_scratch); __ st_ptr(O1_scratch, mh_receiver_slot_addr));

611 612 613 614 615
  __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);

  // for invokeGeneric (only), apply argument and result conversions on the fly
  __ bind(invoke_generic_slow_path);
#ifdef ASSERT
616 617
  if (VerifyMethodHandles) {
    Label L;
618 619 620 621 622 623 624 625 626 627
    __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
    __ cmp(O1_scratch, (int) vmIntrinsics::_invokeGeneric);
    __ brx(Assembler::equal, false, Assembler::pt, L);
    __ delayed()->nop();
    __ stop("bad methodOop::intrinsic_id");
    __ bind(L);
  }
#endif //ASSERT

  // make room on the stack for another pointer:
628
  insert_arg_slots(_masm, 2 * stack_move_unit(), O4_argbase, O1_scratch, O2_scratch, O3_scratch);
629 630 631
  // load up an adapter from the calling type (Java weaves this)
  Register O2_form    = O2_scratch;
  Register O3_adapter = O3_scratch;
632
  __ load_heap_oop(Address(O0_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes,               O1_scratch)), O2_form);
633 634
  __ load_heap_oop(Address(O2_form,  __ delayed_value(java_lang_invoke_MethodTypeForm::genericInvoker_offset_in_bytes, O1_scratch)), O3_adapter);
  __ verify_oop(O3_adapter);
635 636 637 638
  __ st_ptr(O3_adapter, Address(O4_argbase, 1 * Interpreter::stackElementSize));
  // As a trusted first argument, pass the type being called, so the adapter knows
  // the actual types of the arguments and return values.
  // (Generic invokers are shared among form-families of method-type.)
639
  __ st_ptr(O0_mtype,   Address(O4_argbase, 0 * Interpreter::stackElementSize));
640 641 642
  // FIXME: assert that O3_adapter is of the right method-type.
  __ mov(O3_adapter, G3_method_handle);
  trace_method_handle(_masm, "invokeGeneric");
T
twisti 已提交
643 644 645 646 647
  __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);

  return entry_point;
}

648 649 650 651 652 653 654 655
// Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
static RegisterOrConstant constant(int value) {
  return RegisterOrConstant(value);
}

static void load_vmargslot(MacroAssembler* _masm, Address vmargslot_addr, Register result) {
  __ ldsw(vmargslot_addr, result);
}
T
twisti 已提交
656

657 658 659 660 661 662 663 664 665 666 667 668 669 670
static RegisterOrConstant adjust_SP_and_Gargs_down_by_slots(MacroAssembler* _masm,
                                                            RegisterOrConstant arg_slots,
                                                            Register temp_reg, Register temp2_reg) {
  // Keep the stack pointer 2*wordSize aligned.
  const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
  if (arg_slots.is_constant()) {
    const int        offset = arg_slots.as_constant() << LogBytesPerWord;
    const int masked_offset = round_to(offset, 2 * BytesPerWord);
    const int masked_offset2 = (offset + 1*BytesPerWord) & ~TwoWordAlignmentMask;
    assert(masked_offset == masked_offset2, "must agree");
    __ sub(Gargs,        offset, Gargs);
    __ sub(SP,    masked_offset, SP   );
    return offset;
  } else {
T
twisti 已提交
671
#ifdef ASSERT
672 673 674 675 676 677 678 679
    {
      Label L_ok;
      __ cmp(arg_slots.as_register(), 0);
      __ br(Assembler::greaterEqual, false, Assembler::pt, L_ok);
      __ delayed()->nop();
      __ stop("negative arg_slots");
      __ bind(L_ok);
    }
T
twisti 已提交
680
#endif
681 682 683 684 685 686 687
    __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
    __ add( temp_reg,  1*BytesPerWord,       temp2_reg);
    __ andn(temp2_reg, TwoWordAlignmentMask, temp2_reg);
    __ sub(Gargs, temp_reg,  Gargs);
    __ sub(SP,    temp2_reg, SP   );
    return temp_reg;
  }
T
twisti 已提交
688 689
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
static RegisterOrConstant adjust_SP_and_Gargs_up_by_slots(MacroAssembler* _masm,
                                                          RegisterOrConstant arg_slots,
                                                          Register temp_reg, Register temp2_reg) {
  // Keep the stack pointer 2*wordSize aligned.
  const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
  if (arg_slots.is_constant()) {
    const int        offset = arg_slots.as_constant() << LogBytesPerWord;
    const int masked_offset = offset & ~TwoWordAlignmentMask;
    __ add(Gargs,        offset, Gargs);
    __ add(SP,    masked_offset, SP   );
    return offset;
  } else {
    __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
    __ andn(temp_reg, TwoWordAlignmentMask, temp2_reg);
    __ add(Gargs, temp_reg,  Gargs);
    __ add(SP,    temp2_reg, SP   );
    return temp_reg;
  }
}
T
twisti 已提交
709 710

// Helper to insert argument slots into the stack.
711 712 713
// arg_slots must be a multiple of stack_move_unit() and < 0
// argslot_reg is decremented to point to the new (shifted) location of the argslot
// But, temp_reg ends up holding the original value of argslot_reg.
T
twisti 已提交
714 715 716 717
void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
                                     RegisterOrConstant arg_slots,
                                     Register argslot_reg,
                                     Register temp_reg, Register temp2_reg, Register temp3_reg) {
718 719 720 721
  // allow constant zero
  if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
    return;

T
twisti 已提交
722 723 724
  assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
                             (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));

725 726 727 728 729
  BLOCK_COMMENT("insert_arg_slots {");
  if (VerifyMethodHandles)
    verify_argslot(_masm, argslot_reg, temp_reg, "insertion point must fall within current frame");
  if (VerifyMethodHandles)
    verify_stack_move(_masm, arg_slots, -1);
T
twisti 已提交
730 731 732 733 734 735 736 737 738 739

  // Make space on the stack for the inserted argument(s).
  // Then pull down everything shallower than argslot_reg.
  // The stacked return address gets pulled down with everything else.
  // That is, copy [sp, argslot) downward by -size words.  In pseudo-code:
  //   sp -= size;
  //   for (temp = sp + size; temp < argslot; temp++)
  //     temp[-size] = temp[0]
  //   argslot -= size;

740 741 742
  // offset is temp3_reg in case of arg_slots being a register.
  RegisterOrConstant offset = adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
  __ sub(Gargs, offset, temp_reg);  // source pointer for copy
T
twisti 已提交
743 744 745

  {
    Label loop;
746
    __ BIND(loop);
T
twisti 已提交
747
    // pull one word down each time through the loop
748 749
    __ ld_ptr(           Address(temp_reg, 0     ), temp2_reg);
    __ st_ptr(temp2_reg, Address(temp_reg, offset)           );
T
twisti 已提交
750 751
    __ add(temp_reg, wordSize, temp_reg);
    __ cmp(temp_reg, argslot_reg);
752
    __ brx(Assembler::lessUnsigned, false, Assembler::pt, loop);
T
twisti 已提交
753 754 755 756 757
    __ delayed()->nop();  // FILLME
  }

  // Now move the argslot down, to point to the opened-up space.
  __ add(argslot_reg, offset, argslot_reg);
758
  BLOCK_COMMENT("} insert_arg_slots");
T
twisti 已提交
759 760 761 762
}


// Helper to remove argument slots from the stack.
763
// arg_slots must be a multiple of stack_move_unit() and > 0
T
twisti 已提交
764 765 766 767
void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
                                     RegisterOrConstant arg_slots,
                                     Register argslot_reg,
                                     Register temp_reg, Register temp2_reg, Register temp3_reg) {
768 769 770
  // allow constant zero
  if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
    return;
T
twisti 已提交
771 772 773
  assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
                             (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));

774
  BLOCK_COMMENT("remove_arg_slots {");
775 776 777 778 779 780
  if (VerifyMethodHandles)
    verify_argslots(_masm, arg_slots, argslot_reg, temp_reg, temp2_reg, false,
                    "deleted argument(s) must fall within current frame");
  if (VerifyMethodHandles)
    verify_stack_move(_masm, arg_slots, +1);

T
twisti 已提交
781 782 783 784 785 786 787 788
  // Pull up everything shallower than argslot.
  // Then remove the excess space on the stack.
  // The stacked return address gets pulled up with everything else.
  // That is, copy [sp, argslot) upward by size words.  In pseudo-code:
  //   for (temp = argslot-1; temp >= sp; --temp)
  //     temp[size] = temp[0]
  //   argslot += size;
  //   sp += size;
789 790

  RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
T
twisti 已提交
791
  __ sub(argslot_reg, wordSize, temp_reg);  // source pointer for copy
792

T
twisti 已提交
793
  {
794 795
    Label L_loop;
    __ BIND(L_loop);
T
twisti 已提交
796
    // pull one word up each time through the loop
797 798
    __ ld_ptr(           Address(temp_reg, 0     ), temp2_reg);
    __ st_ptr(temp2_reg, Address(temp_reg, offset)           );
T
twisti 已提交
799 800
    __ sub(temp_reg, wordSize, temp_reg);
    __ cmp(temp_reg, Gargs);
801
    __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, L_loop);
T
twisti 已提交
802 803 804 805 806 807
    __ delayed()->nop();  // FILLME
  }

  // And adjust the argslot address to point at the deletion point.
  __ add(argslot_reg, offset, argslot_reg);

808 809 810
  // We don't need the offset at this point anymore, just adjust SP and Gargs.
  (void) adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);

811
  BLOCK_COMMENT("} remove_arg_slots");
812 813
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
// Helper to copy argument slots to the top of the stack.
// The sequence starts with argslot_reg and is counted by slot_count
// slot_count must be a multiple of stack_move_unit() and >= 0
// This function blows the temps but does not change argslot_reg.
void MethodHandles::push_arg_slots(MacroAssembler* _masm,
                                   Register argslot_reg,
                                   RegisterOrConstant slot_count,
                                   Register temp_reg, Register temp2_reg) {
  // allow constant zero
  if (slot_count.is_constant() && slot_count.as_constant() == 0)
    return;
  assert_different_registers(argslot_reg, temp_reg, temp2_reg,
                             (!slot_count.is_register() ? Gargs : slot_count.as_register()),
                             SP);
  assert(Interpreter::stackElementSize == wordSize, "else change this code");

  BLOCK_COMMENT("push_arg_slots {");
  if (VerifyMethodHandles)
    verify_stack_move(_masm, slot_count, 0);

  RegisterOrConstant offset = adjust_SP_and_Gargs_down_by_slots(_masm, slot_count, temp2_reg, temp_reg);

  if (slot_count.is_constant()) {
    for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
      __ ld_ptr(          Address(argslot_reg, i * wordSize), temp_reg);
      __ st_ptr(temp_reg, Address(Gargs,       i * wordSize));
    }
  } else {
    Label L_plural, L_loop, L_break;
    // Emit code to dynamically check for the common cases, zero and one slot.
    __ cmp(slot_count.as_register(), (int32_t) 1);
    __ br(Assembler::greater, false, Assembler::pn, L_plural);
    __ delayed()->nop();
    __ br(Assembler::less, false, Assembler::pn, L_break);
    __ delayed()->nop();
    __ ld_ptr(          Address(argslot_reg, 0), temp_reg);
    __ st_ptr(temp_reg, Address(Gargs,       0));
    __ ba(false, L_break);
    __ delayed()->nop();  // FILLME
    __ BIND(L_plural);

    // Loop for 2 or more:
    //   top = &argslot[slot_count]
    //   while (top > argslot)  *(--Gargs) = *(--top)
    Register top_reg = temp_reg;
    __ add(argslot_reg, offset, top_reg);
    __ add(Gargs,       offset, Gargs  );  // move back up again so we can go down
    __ BIND(L_loop);
    __ sub(top_reg, wordSize, top_reg);
    __ sub(Gargs,   wordSize, Gargs  );
    __ ld_ptr(           Address(top_reg, 0), temp2_reg);
    __ st_ptr(temp2_reg, Address(Gargs,   0));
    __ cmp(top_reg, argslot_reg);
    __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
    __ delayed()->nop();  // FILLME
    __ BIND(L_break);
  }
  BLOCK_COMMENT("} push_arg_slots");
}

// in-place movement; no change to Gargs
// blows temp_reg, temp2_reg
void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
                                      Register bottom_reg,  // invariant
                                      Address  top_addr,    // can use temp_reg
                                      RegisterOrConstant positive_distance_in_slots,  // destroyed if register
                                      Register temp_reg, Register temp2_reg) {
  assert_different_registers(bottom_reg,
                             temp_reg, temp2_reg,
                             positive_distance_in_slots.register_or_noreg());
  BLOCK_COMMENT("move_arg_slots_up {");
  Label L_loop, L_break;
  Register top_reg = temp_reg;
  if (!top_addr.is_same_address(Address(top_reg, 0))) {
    __ add(top_addr, top_reg);
  }
  // Detect empty (or broken) loop:
#ifdef ASSERT
  if (VerifyMethodHandles) {
    // Verify that &bottom < &top (non-empty interval)
    Label L_ok, L_bad;
    if (positive_distance_in_slots.is_register()) {
      __ cmp(positive_distance_in_slots.as_register(), (int32_t) 0);
      __ br(Assembler::lessEqual, false, Assembler::pn, L_bad);
      __ delayed()->nop();
    }
    __ cmp(bottom_reg, top_reg);
    __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_ok);
    __ delayed()->nop();
    __ BIND(L_bad);
    __ stop("valid bounds (copy up)");
    __ BIND(L_ok);
  }
#endif
  __ cmp(bottom_reg, top_reg);
  __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pn, L_break);
  __ delayed()->nop();
  // work top down to bottom, copying contiguous data upwards
  // In pseudo-code:
  //   while (--top >= bottom) *(top + distance) = *(top + 0);
  RegisterOrConstant offset = __ argument_offset(positive_distance_in_slots, positive_distance_in_slots.register_or_noreg());
  __ BIND(L_loop);
  __ sub(top_reg, wordSize, top_reg);
  __ ld_ptr(           Address(top_reg, 0     ), temp2_reg);
  __ st_ptr(temp2_reg, Address(top_reg, offset)           );
  __ cmp(top_reg, bottom_reg);
  __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
  __ delayed()->nop();  // FILLME
  assert(Interpreter::stackElementSize == wordSize, "else change loop");
  __ BIND(L_break);
  BLOCK_COMMENT("} move_arg_slots_up");
}

// in-place movement; no change to rsp
// blows temp_reg, temp2_reg
void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
                                        Address  bottom_addr,  // can use temp_reg
                                        Register top_reg,      // invariant
                                        RegisterOrConstant negative_distance_in_slots,  // destroyed if register
                                        Register temp_reg, Register temp2_reg) {
  assert_different_registers(top_reg,
                             negative_distance_in_slots.register_or_noreg(),
                             temp_reg, temp2_reg);
  BLOCK_COMMENT("move_arg_slots_down {");
  Label L_loop, L_break;
  Register bottom_reg = temp_reg;
  if (!bottom_addr.is_same_address(Address(bottom_reg, 0))) {
    __ add(bottom_addr, bottom_reg);
  }
  // Detect empty (or broken) loop:
#ifdef ASSERT
  assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
  if (VerifyMethodHandles) {
    // Verify that &bottom < &top (non-empty interval)
    Label L_ok, L_bad;
    if (negative_distance_in_slots.is_register()) {
      __ cmp(negative_distance_in_slots.as_register(), (int32_t) 0);
      __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
      __ delayed()->nop();
    }
    __ cmp(bottom_reg, top_reg);
    __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_ok);
    __ delayed()->nop();
    __ BIND(L_bad);
    __ stop("valid bounds (copy down)");
    __ BIND(L_ok);
  }
#endif
  __ cmp(bottom_reg, top_reg);
  __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pn, L_break);
  __ delayed()->nop();
  // work bottom up to top, copying contiguous data downwards
  // In pseudo-code:
  //   while (bottom < top) *(bottom - distance) = *(bottom + 0), bottom++;
  RegisterOrConstant offset = __ argument_offset(negative_distance_in_slots, negative_distance_in_slots.register_or_noreg());
  __ BIND(L_loop);
  __ ld_ptr(           Address(bottom_reg, 0     ), temp2_reg);
  __ st_ptr(temp2_reg, Address(bottom_reg, offset)           );
  __ add(bottom_reg, wordSize, bottom_reg);
  __ cmp(bottom_reg, top_reg);
  __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_loop);
  __ delayed()->nop();  // FILLME
  assert(Interpreter::stackElementSize == wordSize, "else change loop");
  __ BIND(L_break);
  BLOCK_COMMENT("} move_arg_slots_down");
}

// Copy from a field or array element to a stacked argument slot.
// is_element (ignored) says whether caller is loading an array element instead of an instance field.
void MethodHandles::move_typed_arg(MacroAssembler* _masm,
                                   BasicType type, bool is_element,
                                   Address value_src, Address slot_dest,
                                   Register temp_reg) {
  assert(!slot_dest.uses(temp_reg), "must be different register");
  BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
  if (type == T_OBJECT || type == T_ARRAY) {
    __ load_heap_oop(value_src, temp_reg);
    __ verify_oop(temp_reg);
    __ st_ptr(temp_reg, slot_dest);
  } else if (type != T_VOID) {
    int  arg_size      = type2aelembytes(type);
    bool arg_is_signed = is_signed_subword_type(type);
    int  slot_size     = is_subword_type(type) ? type2aelembytes(T_INT) : arg_size;  // store int sub-words as int
    __ load_sized_value( value_src, temp_reg, arg_size, arg_is_signed);
    __ store_sized_value(temp_reg, slot_dest, slot_size              );
  }
  BLOCK_COMMENT("} move_typed_arg");
}

// Cf. TemplateInterpreterGenerator::generate_return_entry_for and
// InterpreterMacroAssembler::save_return_value
void MethodHandles::move_return_value(MacroAssembler* _masm, BasicType type,
                                      Address return_slot) {
  BLOCK_COMMENT("move_return_value {");
  // Look at the type and pull the value out of the corresponding register.
  if (type == T_VOID) {
    // nothing to do
  } else if (type == T_OBJECT) {
    __ verify_oop(O0);
    __ st_ptr(O0, return_slot);
  } else if (type == T_INT || is_subword_type(type)) {
    int type_size = type2aelembytes(T_INT);
    __ store_sized_value(O0, return_slot, type_size);
  } else if (type == T_LONG) {
    // store the value by parts
    // Note: We assume longs are continguous (if misaligned) on the interpreter stack.
#if !defined(_LP64) && defined(COMPILER2)
    __ stx(G1, return_slot);
#else
  #ifdef _LP64
    __ stx(O0, return_slot);
  #else
    if (return_slot.has_disp()) {
      // The displacement is a constant
      __ st(O0, return_slot);
      __ st(O1, return_slot.plus_disp(Interpreter::stackElementSize));
    } else {
      __ std(O0, return_slot);
    }
  #endif
#endif
  } else if (type == T_FLOAT) {
    __ stf(FloatRegisterImpl::S, Ftos_f, return_slot);
  } else if (type == T_DOUBLE) {
    __ stf(FloatRegisterImpl::D, Ftos_f, return_slot);
  } else {
    ShouldNotReachHere();
  }
  BLOCK_COMMENT("} move_return_value");
}
T
twisti 已提交
1044 1045 1046 1047

#ifndef PRODUCT
extern "C" void print_method_handle(oop mh);
void trace_method_handle_stub(const char* adaptername,
1048 1049
                              oopDesc* mh,
                              intptr_t* saved_sp) {
1050
  bool has_mh = (strstr(adaptername, "return/") == NULL);  // return adapters don't have mh
1051
  tty->print_cr("MH %s mh="INTPTR_FORMAT " saved_sp=" INTPTR_FORMAT, adaptername, (intptr_t) mh, saved_sp);
1052 1053
  if (has_mh)
    print_method_handle(mh);
T
twisti 已提交
1054
}
1055 1056 1057 1058 1059 1060 1061
void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
  if (!TraceMethodHandles)  return;
  BLOCK_COMMENT("trace_method_handle {");
  // save: Gargs, O5_savedSP
  __ save_frame(16);
  __ set((intptr_t) adaptername, O0);
  __ mov(G3_method_handle, O1);
1062
  __ mov(I5_savedSP, O2);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  __ mov(G3_method_handle, L3);
  __ mov(Gargs, L4);
  __ mov(G5_method_type, L5);
  __ call_VM_leaf(L7, CAST_FROM_FN_PTR(address, trace_method_handle_stub));

  __ mov(L3, G3_method_handle);
  __ mov(L4, Gargs);
  __ mov(L5, G5_method_type);
  __ restore();
  BLOCK_COMMENT("} trace_method_handle");
}
T
twisti 已提交
1074 1075
#endif // PRODUCT

1076 1077
// which conversion op types are implemented here?
int MethodHandles::adapter_conversion_ops_supported_mask() {
1078 1079 1080 1081 1082
  return ((1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM)
1083
          // OP_PRIM_TO_REF is below...
1084 1085 1086 1087
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS)
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS)
1088 1089 1090 1091 1092 1093 1094 1095 1096
          // OP_COLLECT_ARGS is below...
         |(1<<java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS)
         |(!UseRicochetFrames ? 0 :
           java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() <= 0 ? 0 :
           ((1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF)
           |(1<<java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS)
           |(1<<java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS)
           )
          )
1097 1098
         );
}
T
twisti 已提交
1099 1100 1101 1102

//------------------------------------------------------------------------------
// MethodHandles::generate_method_handle_stub
//
1103 1104
// Generate an "entry" field for a method handle.
// This determines how the method handle will respond to calls.
1105
void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek) {
1106 1107
  MethodHandles::EntryKind ek_orig = ek_original_kind(ek);

T
twisti 已提交
1108 1109 1110 1111 1112 1113
  // Here is the register state during an interpreted call,
  // as set up by generate_method_handle_interpreter_entry():
  // - G5: garbage temp (was MethodHandle.invoke methodOop, unused)
  // - G3: receiver method handle
  // - O5_savedSP: sender SP (must preserve)

1114
  const Register O0_scratch = O0;
1115 1116 1117
  const Register O1_scratch = O1;
  const Register O2_scratch = O2;
  const Register O3_scratch = O3;
1118 1119 1120 1121 1122
  const Register O4_scratch = O4;
  const Register G5_scratch = G5;

  // Often used names:
  const Register O0_argslot = O0;
1123

1124
  // Argument registers for _raise_exception:
1125 1126 1127
  const Register O0_code     = O0;
  const Register O1_actual   = O1;
  const Register O2_required = O2;
T
twisti 已提交
1128

1129
  guarantee(java_lang_invoke_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
T
twisti 已提交
1130 1131

  // Some handy addresses:
1132
  Address G3_mh_vmtarget(   G3_method_handle, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
T
twisti 已提交
1133

1134
  Address G3_dmh_vmindex(   G3_method_handle, java_lang_invoke_DirectMethodHandle::vmindex_offset_in_bytes());
T
twisti 已提交
1135

1136 1137
  Address G3_bmh_vmargslot( G3_method_handle, java_lang_invoke_BoundMethodHandle::vmargslot_offset_in_bytes());
  Address G3_bmh_argument(  G3_method_handle, java_lang_invoke_BoundMethodHandle::argument_offset_in_bytes());
T
twisti 已提交
1138

1139 1140 1141
  Address G3_amh_vmargslot( G3_method_handle, java_lang_invoke_AdapterMethodHandle::vmargslot_offset_in_bytes());
  Address G3_amh_argument ( G3_method_handle, java_lang_invoke_AdapterMethodHandle::argument_offset_in_bytes());
  Address G3_amh_conversion(G3_method_handle, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
T
twisti 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

  const int java_mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();

  if (have_entry(ek)) {
    __ nop();  // empty stubs make SG sick
    return;
  }

  address interp_entry = __ pc();

1152
  trace_method_handle(_masm, entry_name(ek));
T
twisti 已提交
1153

1154 1155
  BLOCK_COMMENT(err_msg("Entry %s {", entry_name(ek)));

T
twisti 已提交
1156 1157 1158 1159
  switch ((int) ek) {
  case _raise_exception:
    {
      // Not a real MH entry, but rather shared code for raising an
1160 1161
      // exception.  For sharing purposes the arguments are passed into registers
      // and then placed in the intepreter calling convention here.
1162
      assert(raise_exception_method(), "must be set");
1163
      assert(raise_exception_method()->from_compiled_entry(), "method must be linked");
T
twisti 已提交
1164 1165 1166 1167

      __ set(AddressLiteral((address) &_raise_exception_method), G5_method);
      __ ld_ptr(Address(G5_method, 0), G5_method);

1168
      const int jobject_oop_offset = 0;
T
twisti 已提交
1169 1170
      __ ld_ptr(Address(G5_method, jobject_oop_offset), G5_method);

1171 1172 1173 1174 1175 1176
      adjust_SP_and_Gargs_down_by_slots(_masm, 3, noreg, noreg);

      __ st_ptr(O0_code,     __ argument_address(constant(2), noreg, 0));
      __ st_ptr(O1_actual,   __ argument_address(constant(1), noreg, 0));
      __ st_ptr(O2_required, __ argument_address(constant(0), noreg, 0));
      jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
T
twisti 已提交
1177 1178 1179 1180 1181 1182
    }
    break;

  case _invokestatic_mh:
  case _invokespecial_mh:
    {
1183
      __ load_heap_oop(G3_mh_vmtarget, G5_method);  // target is a methodOop
T
twisti 已提交
1184 1185 1186 1187 1188
      // Same as TemplateTable::invokestatic or invokespecial,
      // minus the CP setup and profiling:
      if (ek == _invokespecial_mh) {
        // Must load & check the first argument before entering the target method.
        __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1189
        __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
T
twisti 已提交
1190 1191 1192
        __ null_check(G3_method_handle);
        __ verify_oop(G3_method_handle);
      }
1193
      jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
T
twisti 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    }
    break;

  case _invokevirtual_mh:
    {
      // Same as TemplateTable::invokevirtual,
      // minus the CP setup and profiling:

      // Pick out the vtable index and receiver offset from the MH,
      // and then we can discard it:
1204
      Register O2_index = O2_scratch;
T
twisti 已提交
1205
      __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1206
      __ ldsw(G3_dmh_vmindex, O2_index);
T
twisti 已提交
1207
      // Note:  The verifier allows us to ignore G3_mh_vmtarget.
1208
      __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
T
twisti 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
      __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());

      // Get receiver klass:
      Register O0_klass = O0_argslot;
      __ load_klass(G3_method_handle, O0_klass);
      __ verify_oop(O0_klass);

      // Get target methodOop & entry point:
      const int base = instanceKlass::vtable_start_offset() * wordSize;
      assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");

1220 1221
      __ sll_ptr(O2_index, LogBytesPerWord, O2_index);
      __ add(O0_klass, O2_index, O0_klass);
T
twisti 已提交
1222 1223 1224
      Address vtable_entry_addr(O0_klass, base + vtableEntry::method_offset_in_bytes());
      __ ld_ptr(vtable_entry_addr, G5_method);

1225
      jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
T
twisti 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234
    }
    break;

  case _invokeinterface_mh:
    {
      // Same as TemplateTable::invokeinterface,
      // minus the CP setup and profiling:
      __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
      Register O1_intf  = O1_scratch;
1235
      Register G5_index = G5_scratch;
1236
      __ load_heap_oop(G3_mh_vmtarget, O1_intf);
T
twisti 已提交
1237
      __ ldsw(G3_dmh_vmindex, G5_index);
1238
      __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
T
twisti 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
      __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());

      // Get receiver klass:
      Register O0_klass = O0_argslot;
      __ load_klass(G3_method_handle, O0_klass);
      __ verify_oop(O0_klass);

      // Get interface:
      Label no_such_interface;
      __ verify_oop(O1_intf);
      __ lookup_interface_method(O0_klass, O1_intf,
                                 // Note: next two args must be the same:
                                 G5_index, G5_method,
                                 O2_scratch,
                                 O3_scratch,
                                 no_such_interface);

1256
      jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
T
twisti 已提交
1257 1258 1259 1260 1261

      __ bind(no_such_interface);
      // Throw an exception.
      // For historical reasons, it will be IncompatibleClassChangeError.
      __ unimplemented("not tested yet");
1262 1263 1264 1265
      __ ld_ptr(Address(O1_intf, java_mirror_offset), O2_required);  // required interface
      __ mov(   O0_klass,                             O1_actual);    // bad receiver
      __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
      __ delayed()->mov(Bytecodes::_invokeinterface,  O0_code);      // who is complaining?
T
twisti 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
    }
    break;

  case _bound_ref_mh:
  case _bound_int_mh:
  case _bound_long_mh:
  case _bound_ref_direct_mh:
  case _bound_int_direct_mh:
  case _bound_long_direct_mh:
    {
      const bool direct_to_method = (ek >= _bound_ref_direct_mh);
1277 1278
      BasicType arg_type  = ek_bound_mh_arg_type(ek);
      int       arg_slots = type2size[arg_type];
T
twisti 已提交
1279 1280

      // Make room for the new argument:
1281 1282
      load_vmargslot(_masm, G3_bmh_vmargslot, O0_argslot);
      __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
T
twisti 已提交
1283

1284
      insert_arg_slots(_masm, arg_slots * stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
T
twisti 已提交
1285 1286

      // Store bound argument into the new stack slot:
1287
      __ load_heap_oop(G3_bmh_argument, O1_scratch);
T
twisti 已提交
1288 1289 1290 1291
      if (arg_type == T_OBJECT) {
        __ st_ptr(O1_scratch, Address(O0_argslot, 0));
      } else {
        Address prim_value_addr(O1_scratch, java_lang_boxing_object::value_offset_in_bytes(arg_type));
1292 1293 1294 1295
        move_typed_arg(_masm, arg_type, false,
                       prim_value_addr,
                       Address(O0_argslot, 0),
                       O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
T
twisti 已提交
1296 1297 1298
      }

      if (direct_to_method) {
1299
        __ load_heap_oop(G3_mh_vmtarget, G5_method);  // target is a methodOop
1300
        jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
T
twisti 已提交
1301
      } else {
1302
        __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);  // target is a methodOop
T
twisti 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311
        __ verify_oop(G3_method_handle);
        __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
      }
    }
    break;

  case _adapter_retype_only:
  case _adapter_retype_raw:
    // Immediately jump to the next MH layer:
1312
    __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1313
    __ verify_oop(G3_method_handle);
T
twisti 已提交
1314 1315 1316 1317 1318 1319 1320 1321
    __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    // This is OK when all parameter types widen.
    // It is also OK when a return type narrows.
    break;

  case _adapter_check_cast:
    {
      // Check a reference argument before jumping to the next layer of MH:
1322 1323
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
      Address vmarg = __ argument_address(O0_argslot, O0_argslot);
T
twisti 已提交
1324 1325

      // What class are we casting to?
1326 1327 1328 1329 1330 1331 1332 1333
      Register O1_klass = O1_scratch;  // Interesting AMH data.
      __ load_heap_oop(G3_amh_argument, O1_klass);  // This is a Class object!
      load_klass_from_Class(_masm, O1_klass, O2_scratch, O3_scratch);

      Label L_done;
      __ ld_ptr(vmarg, O2_scratch);
      __ tst(O2_scratch);
      __ brx(Assembler::zero, false, Assembler::pn, L_done);  // No cast if null.
T
twisti 已提交
1334
      __ delayed()->nop();
1335
      __ load_klass(O2_scratch, O2_scratch);
T
twisti 已提交
1336 1337

      // Live at this point:
1338
      // - O0_argslot      :  argslot index in vmarg; may be required in the failing path
1339 1340
      // - O1_klass        :  klass required by the target method
      // - O2_scratch      :  argument klass to test
T
twisti 已提交
1341
      // - G3_method_handle:  adapter method handle
1342
      __ check_klass_subtype(O2_scratch, O1_klass, O3_scratch, O4_scratch, L_done);
T
twisti 已提交
1343 1344

      // If we get here, the type check failed!
1345 1346 1347 1348
      __ load_heap_oop(G3_amh_argument,        O2_required);  // required class
      __ ld_ptr(       vmarg,                  O1_actual);    // bad object
      __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
      __ delayed()->mov(Bytecodes::_checkcast, O0_code);      // who is complaining?
T
twisti 已提交
1349

1350
      __ BIND(L_done);
T
twisti 已提交
1351
      // Get the new MH:
1352
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_prim_to_prim:
  case _adapter_ref_to_prim:
    // Handled completely by optimized cases.
    __ stop("init_AdapterMethodHandle should not issue this");
    break;

  case _adapter_opt_i2i:        // optimized subcase of adapt_prim_to_prim
//case _adapter_opt_f2i:        // optimized subcase of adapt_prim_to_prim
  case _adapter_opt_l2i:        // optimized subcase of adapt_prim_to_prim
  case _adapter_opt_unboxi:     // optimized subcase of adapt_ref_to_prim
    {
      // Perform an in-place conversion to int or an int subword.
1369
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
T
twisti 已提交
1370
      Address value;
1371
      Address vmarg;
T
twisti 已提交
1372 1373 1374 1375
      bool value_left_justified = false;

      switch (ek) {
      case _adapter_opt_i2i:
1376
        value = vmarg = __ argument_address(O0_argslot, O0_argslot);
T
twisti 已提交
1377
        break;
1378 1379 1380
      case _adapter_opt_l2i:
        {
          // just delete the extra slot
1381 1382 1383 1384
#ifdef _LP64
          // In V9, longs are given 2 64-bit slots in the interpreter, but the
          // data is passed in only 1 slot.
          // Keep the second slot.
1385
          __ add(__ argument_address(O0_argslot, O0_argslot, -1), O0_argslot);
1386 1387 1388 1389 1390
          remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
          value = Address(O0_argslot, 4);  // Get least-significant 32-bit of 64-bit value.
          vmarg = Address(O0_argslot, Interpreter::stackElementSize);
#else
          // Keep the first slot.
1391
          __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1392
          remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1393 1394 1395
          value = Address(O0_argslot, 0);
          vmarg = value;
#endif
1396 1397
        }
        break;
T
twisti 已提交
1398 1399
      case _adapter_opt_unboxi:
        {
1400
          vmarg = __ argument_address(O0_argslot, O0_argslot);
T
twisti 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
          // Load the value up from the heap.
          __ ld_ptr(vmarg, O1_scratch);
          int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
#ifdef ASSERT
          for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
            if (is_subword_type(BasicType(bt)))
              assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
          }
#endif
          __ null_check(O1_scratch, value_offset);
          value = Address(O1_scratch, value_offset);
#ifdef _BIG_ENDIAN
          // Values stored in objects are packed.
          value_left_justified = true;
#endif
        }
        break;
      default:
        ShouldNotReachHere();
      }

      // This check is required on _BIG_ENDIAN
1423
      Register G5_vminfo = G5_scratch;
T
twisti 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
      __ ldsw(G3_amh_conversion, G5_vminfo);
      assert(CONV_VMINFO_SHIFT == 0, "preshifted");

      // Original 32-bit vmdata word must be of this form:
      // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
      __ lduw(value, O1_scratch);
      if (!value_left_justified)
        __ sll(O1_scratch, G5_vminfo, O1_scratch);
      Label zero_extend, done;
      __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
      __ br(Assembler::zero, false, Assembler::pn, zero_extend);
      __ delayed()->nop();

      // this path is taken for int->byte, int->short
      __ sra(O1_scratch, G5_vminfo, O1_scratch);
      __ ba(false, done);
      __ delayed()->nop();

      __ bind(zero_extend);
      // this is taken for int->char
      __ srl(O1_scratch, G5_vminfo, O1_scratch);

      __ bind(done);
      __ st(O1_scratch, vmarg);

      // Get the new MH:
1450
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1451 1452 1453 1454 1455 1456 1457 1458
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_opt_i2l:        // optimized subcase of adapt_prim_to_prim
  case _adapter_opt_unboxl:     // optimized subcase of adapt_ref_to_prim
    {
      // Perform an in-place int-to-long or ref-to-long conversion.
1459
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
T
twisti 已提交
1460 1461 1462

      // On big-endian machine we duplicate the slot and store the MSW
      // in the first slot.
1463
      __ add(__ argument_address(O0_argslot, O0_argslot, 1), O0_argslot);
T
twisti 已提交
1464

1465
      insert_arg_slots(_masm, stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
T
twisti 已提交
1466 1467

      Address arg_lsw(O0_argslot, 0);
1468
      Address arg_msw(O0_argslot, -Interpreter::stackElementSize);
T
twisti 已提交
1469 1470 1471 1472

      switch (ek) {
      case _adapter_opt_i2l:
        {
1473 1474 1475 1476 1477 1478 1479
#ifdef _LP64
          __ ldsw(arg_lsw, O2_scratch);                 // Load LSW sign-extended
#else
          __ ldsw(arg_lsw, O3_scratch);                 // Load LSW sign-extended
          __ srlx(O3_scratch, BitsPerInt, O2_scratch);  // Move MSW value to lower 32-bits for std
#endif
          __ st_long(O2_scratch, arg_msw);              // Uses O2/O3 on !_LP64
T
twisti 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
        }
        break;
      case _adapter_opt_unboxl:
        {
          // Load the value up from the heap.
          __ ld_ptr(arg_lsw, O1_scratch);
          int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
          assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
          __ null_check(O1_scratch, value_offset);
          __ ld_long(Address(O1_scratch, value_offset), O2_scratch);  // Uses O2/O3 on !_LP64
          __ st_long(O2_scratch, arg_msw);
        }
        break;
      default:
        ShouldNotReachHere();
      }

1497
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_opt_f2d:        // optimized subcase of adapt_prim_to_prim
  case _adapter_opt_d2f:        // optimized subcase of adapt_prim_to_prim
    {
      // perform an in-place floating primitive conversion
      __ unimplemented(entry_name(ek));
    }
    break;

  case _adapter_prim_to_ref:
    __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
    break;

  case _adapter_swap_args:
  case _adapter_rot_args:
    // handled completely by optimized cases
    __ stop("init_AdapterMethodHandle should not issue this");
    break;

  case _adapter_opt_swap_1:
  case _adapter_opt_swap_2:
  case _adapter_opt_rot_1_up:
  case _adapter_opt_rot_1_down:
  case _adapter_opt_rot_2_up:
  case _adapter_opt_rot_2_down:
    {
1527 1528
      int swap_slots = ek_adapter_opt_swap_slots(ek);
      int rotate     = ek_adapter_opt_swap_mode(ek);
T
twisti 已提交
1529 1530

      // 'argslot' is the position of the first argument to swap.
1531 1532 1533 1534
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
      __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
      if (VerifyMethodHandles)
        verify_argslot(_masm, O0_argslot, O2_scratch, "swap point must fall within current frame");
T
twisti 已提交
1535 1536 1537

      // 'vminfo' is the second.
      Register O1_destslot = O1_scratch;
1538 1539 1540 1541
      load_conversion_vminfo(_masm, G3_amh_conversion, O1_destslot);
      __ add(__ argument_address(O1_destslot, O1_destslot), O1_destslot);
      if (VerifyMethodHandles)
        verify_argslot(_masm, O1_destslot, O2_scratch, "swap point must fall within current frame");
T
twisti 已提交
1542

1543
      assert(Interpreter::stackElementSize == wordSize, "else rethink use of wordSize here");
T
twisti 已提交
1544
      if (!rotate) {
1545 1546 1547 1548 1549 1550
        // simple swap
        for (int i = 0; i < swap_slots; i++) {
          __ ld_ptr(            Address(O0_argslot,  i * wordSize), O2_scratch);
          __ ld_ptr(            Address(O1_destslot, i * wordSize), O3_scratch);
          __ st_ptr(O3_scratch, Address(O0_argslot,  i * wordSize));
          __ st_ptr(O2_scratch, Address(O1_destslot, i * wordSize));
T
twisti 已提交
1551 1552
        }
      } else {
1553 1554 1555 1556 1557 1558 1559
        // A rotate is actually pair of moves, with an "odd slot" (or pair)
        // changing place with a series of other slots.
        // First, push the "odd slot", which is going to get overwritten
        switch (swap_slots) {
        case 2 :  __ ld_ptr(Address(O0_argslot, 1 * wordSize), O4_scratch); // fall-thru
        case 1 :  __ ld_ptr(Address(O0_argslot, 0 * wordSize), O3_scratch); break;
        default:  ShouldNotReachHere();
T
twisti 已提交
1560 1561
        }
        if (rotate > 0) {
1562 1563 1564 1565 1566 1567 1568
          // Here is rotate > 0:
          // (low mem)                                          (high mem)
          //     | dest:     more_slots...     | arg: odd_slot :arg+1 |
          // =>
          //     | dest: odd_slot | dest+1: more_slots...      :arg+1 |
          // work argslot down to destslot, copying contiguous data upwards
          // pseudo-code:
T
twisti 已提交
1569 1570
          //   argslot  = src_addr - swap_bytes
          //   destslot = dest_addr
1571 1572 1573 1574 1575 1576
          //   while (argslot >= destslot) *(argslot + swap_bytes) = *(argslot + 0), argslot--;
          move_arg_slots_up(_masm,
                            O1_destslot,
                            Address(O0_argslot, 0),
                            swap_slots,
                            O0_argslot, O2_scratch);
T
twisti 已提交
1577
        } else {
1578 1579 1580 1581 1582 1583 1584
          // Here is the other direction, rotate < 0:
          // (low mem)                                          (high mem)
          //     | arg: odd_slot | arg+1: more_slots...       :dest+1 |
          // =>
          //     | arg:    more_slots...     | dest: odd_slot :dest+1 |
          // work argslot up to destslot, copying contiguous data downwards
          // pseudo-code:
T
twisti 已提交
1585 1586
          //   argslot  = src_addr + swap_bytes
          //   destslot = dest_addr
1587
          //   while (argslot <= destslot) *(argslot - swap_bytes) = *(argslot + 0), argslot++;
1588 1589 1590 1591
          // dest_slot denotes an exclusive upper limit
          int limit_bias = OP_ROT_ARGS_DOWN_LIMIT_BIAS;
          if (limit_bias != 0)
            __ add(O1_destslot, - limit_bias * wordSize, O1_destslot);
1592 1593 1594 1595 1596 1597
          move_arg_slots_down(_masm,
                              Address(O0_argslot, swap_slots * wordSize),
                              O1_destslot,
                              -swap_slots,
                              O0_argslot, O2_scratch);

1598
          __ sub(O1_destslot, swap_slots * wordSize, O1_destslot);
T
twisti 已提交
1599
        }
1600 1601 1602 1603 1604
        // pop the original first chunk into the destination slot, now free
        switch (swap_slots) {
        case 2 :  __ st_ptr(O4_scratch, Address(O1_destslot, 1 * wordSize)); // fall-thru
        case 1 :  __ st_ptr(O3_scratch, Address(O1_destslot, 0 * wordSize)); break;
        default:  ShouldNotReachHere();
T
twisti 已提交
1605 1606 1607
        }
      }

1608
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1609 1610 1611 1612 1613 1614 1615
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_dup_args:
    {
      // 'argslot' is the position of the first argument to duplicate.
1616 1617
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
      __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
T
twisti 已提交
1618 1619

      // 'stack_move' is negative number of words to duplicate.
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
      Register O1_stack_move = O1_scratch;
      load_stack_move(_masm, G3_amh_conversion, O1_stack_move);

      if (VerifyMethodHandles) {
        verify_argslots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, true,
                        "copied argument(s) must fall within current frame");
      }

      // insert location is always the bottom of the argument list:
      __ neg(O1_stack_move);
      push_arg_slots(_masm, O0_argslot, O1_stack_move, O2_scratch, O3_scratch);
T
twisti 已提交
1631

1632
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1633 1634 1635 1636 1637 1638 1639
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_drop_args:
    {
      // 'argslot' is the position of the first argument to nuke.
1640 1641
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
      __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
T
twisti 已提交
1642 1643

      // 'stack_move' is number of words to drop.
1644 1645
      Register O1_stack_move = O1_scratch;
      load_stack_move(_masm, G3_amh_conversion, O1_stack_move);
T
twisti 已提交
1646

1647
      remove_arg_slots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, O4_scratch);
T
twisti 已提交
1648

1649
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
T
twisti 已提交
1650 1651 1652 1653 1654
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
    }
    break;

  case _adapter_collect_args:
1655
  case _adapter_fold_args:
T
twisti 已提交
1656 1657 1658 1659 1660
  case _adapter_spread_args:
    // Handled completely by optimized cases.
    __ stop("init_AdapterMethodHandle should not issue this");
    break;

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
  case _adapter_opt_collect_ref:
  case _adapter_opt_collect_int:
  case _adapter_opt_collect_long:
  case _adapter_opt_collect_float:
  case _adapter_opt_collect_double:
  case _adapter_opt_collect_void:
  case _adapter_opt_collect_0_ref:
  case _adapter_opt_collect_1_ref:
  case _adapter_opt_collect_2_ref:
  case _adapter_opt_collect_3_ref:
  case _adapter_opt_collect_4_ref:
  case _adapter_opt_collect_5_ref:
  case _adapter_opt_filter_S0_ref:
  case _adapter_opt_filter_S1_ref:
  case _adapter_opt_filter_S2_ref:
  case _adapter_opt_filter_S3_ref:
  case _adapter_opt_filter_S4_ref:
  case _adapter_opt_filter_S5_ref:
  case _adapter_opt_collect_2_S0_ref:
  case _adapter_opt_collect_2_S1_ref:
  case _adapter_opt_collect_2_S2_ref:
  case _adapter_opt_collect_2_S3_ref:
  case _adapter_opt_collect_2_S4_ref:
  case _adapter_opt_collect_2_S5_ref:
  case _adapter_opt_fold_ref:
  case _adapter_opt_fold_int:
  case _adapter_opt_fold_long:
  case _adapter_opt_fold_float:
  case _adapter_opt_fold_double:
  case _adapter_opt_fold_void:
  case _adapter_opt_fold_1_ref:
  case _adapter_opt_fold_2_ref:
  case _adapter_opt_fold_3_ref:
  case _adapter_opt_fold_4_ref:
  case _adapter_opt_fold_5_ref:
T
twisti 已提交
1696
    {
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
      // Given a fresh incoming stack frame, build a new ricochet frame.
      // On entry, TOS points at a return PC, and FP is the callers frame ptr.
      // RSI/R13 has the caller's exact stack pointer, which we must also preserve.
      // RCX contains an AdapterMethodHandle of the indicated kind.

      // Relevant AMH fields:
      // amh.vmargslot:
      //   points to the trailing edge of the arguments
      //   to filter, collect, or fold.  For a boxing operation,
      //   it points just after the single primitive value.
      // amh.argument:
      //   recursively called MH, on |collect| arguments
      // amh.vmtarget:
      //   final destination MH, on return value, etc.
      // amh.conversion.dest:
      //   tells what is the type of the return value
      //   (not needed here, since dest is also derived from ek)
      // amh.conversion.vminfo:
      //   points to the trailing edge of the return value
      //   when the vmtarget is to be called; this is
      //   equal to vmargslot + (retained ? |collect| : 0)

      // Pass 0 or more argument slots to the recursive target.
      int collect_count_constant = ek_adapter_opt_collect_count(ek);

      // The collected arguments are copied from the saved argument list:
      int collect_slot_constant = ek_adapter_opt_collect_slot(ek);

      assert(ek_orig == _adapter_collect_args ||
             ek_orig == _adapter_fold_args, "");
      bool retain_original_args = (ek_orig == _adapter_fold_args);

      // The return value is replaced (or inserted) at the 'vminfo' argslot.
      // Sometimes we can compute this statically.
      int dest_slot_constant = -1;
      if (!retain_original_args)
        dest_slot_constant = collect_slot_constant;
      else if (collect_slot_constant >= 0 && collect_count_constant >= 0)
        // We are preserving all the arguments, and the return value is prepended,
        // so the return slot is to the left (above) the |collect| sequence.
        dest_slot_constant = collect_slot_constant + collect_count_constant;

      // Replace all those slots by the result of the recursive call.
      // The result type can be one of ref, int, long, float, double, void.
      // In the case of void, nothing is pushed on the stack after return.
      BasicType dest = ek_adapter_opt_collect_type(ek);
      assert(dest == type2wfield[dest], "dest is a stack slot type");
      int dest_count = type2size[dest];
      assert(dest_count == 1 || dest_count == 2 || (dest_count == 0 && dest == T_VOID), "dest has a size");

      // Choose a return continuation.
      EntryKind ek_ret = _adapter_opt_return_any;
      if (dest != T_CONFLICT && OptimizeMethodHandles) {
        switch (dest) {
        case T_INT    : ek_ret = _adapter_opt_return_int;     break;
        case T_LONG   : ek_ret = _adapter_opt_return_long;    break;
        case T_FLOAT  : ek_ret = _adapter_opt_return_float;   break;
        case T_DOUBLE : ek_ret = _adapter_opt_return_double;  break;
        case T_OBJECT : ek_ret = _adapter_opt_return_ref;     break;
        case T_VOID   : ek_ret = _adapter_opt_return_void;    break;
        default       : ShouldNotReachHere();
        }
        if (dest == T_OBJECT && dest_slot_constant >= 0) {
          EntryKind ek_try = EntryKind(_adapter_opt_return_S0_ref + dest_slot_constant);
          if (ek_try <= _adapter_opt_return_LAST &&
              ek_adapter_opt_return_slot(ek_try) == dest_slot_constant) {
            ek_ret = ek_try;
          }
        }
        assert(ek_adapter_opt_return_type(ek_ret) == dest, "");
      }

      // Already pushed:  ... keep1 | collect | keep2 |

      // Push a few extra argument words, if we need them to store the return value.
      {
        int extra_slots = 0;
        if (retain_original_args) {
          extra_slots = dest_count;
        } else if (collect_count_constant == -1) {
          extra_slots = dest_count;  // collect_count might be zero; be generous
        } else if (dest_count > collect_count_constant) {
          extra_slots = (dest_count - collect_count_constant);
        } else {
          // else we know we have enough dead space in |collect| to repurpose for return values
        }
        if (extra_slots != 0) {
          __ sub(SP, round_to(extra_slots, 2) * Interpreter::stackElementSize, SP);
        }
      }

      // Set up Ricochet Frame.
      __ mov(SP, O5_savedSP);  // record SP for the callee

      // One extra (empty) slot for outgoing target MH (see Gargs computation below).
      __ save_frame(2);  // Note: we need to add 2 slots since frame::memory_parameter_word_sp_offset is 23.

      // Note: Gargs is live throughout the following, until we make our recursive call.
      // And the RF saves a copy in L4_saved_args_base.

      RicochetFrame::enter_ricochet_frame(_masm, G3_method_handle, Gargs,
                                          entry(ek_ret)->from_interpreted_entry());

      // Compute argument base:
      // Set up Gargs for current frame, extra (empty) slot is for outgoing target MH (space reserved by save_frame above).
      __ add(FP, STACK_BIAS - (1 * Interpreter::stackElementSize), Gargs);

      // Now pushed:  ... keep1 | collect | keep2 | extra | [RF]

#ifdef ASSERT
      if (VerifyMethodHandles && dest != T_CONFLICT) {
        BLOCK_COMMENT("verify AMH.conv.dest {");
        extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O1_scratch);
        Label L_dest_ok;
        __ cmp(O1_scratch, (int) dest);
        __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
        __ delayed()->nop();
        if (dest == T_INT) {
          for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
            if (is_subword_type(BasicType(bt))) {
              __ cmp(O1_scratch, (int) bt);
              __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
              __ delayed()->nop();
            }
          }
        }
        __ stop("bad dest in AMH.conv");
        __ BIND(L_dest_ok);
        BLOCK_COMMENT("} verify AMH.conv.dest");
      }
#endif //ASSERT

      // Find out where the original copy of the recursive argument sequence begins.
      Register O0_coll = O0_scratch;
      {
        RegisterOrConstant collect_slot = collect_slot_constant;
        if (collect_slot_constant == -1) {
          load_vmargslot(_masm, G3_amh_vmargslot, O1_scratch);
          collect_slot = O1_scratch;
        }
        // collect_slot might be 0, but we need the move anyway.
        __ add(RicochetFrame::L4_saved_args_base, __ argument_offset(collect_slot, collect_slot.register_or_noreg()), O0_coll);
        // O0_coll now points at the trailing edge of |collect| and leading edge of |keep2|
      }

      // Replace the old AMH with the recursive MH.  (No going back now.)
      // In the case of a boxing call, the recursive call is to a 'boxer' method,
      // such as Integer.valueOf or Long.valueOf.  In the case of a filter
      // or collect call, it will take one or more arguments, transform them,
      // and return some result, to store back into argument_base[vminfo].
      __ load_heap_oop(G3_amh_argument, G3_method_handle);
      if (VerifyMethodHandles)  verify_method_handle(_masm, G3_method_handle, O1_scratch, O2_scratch);

      // Calculate |collect|, the number of arguments we are collecting.
      Register O1_collect_count = O1_scratch;
      RegisterOrConstant collect_count;
      if (collect_count_constant < 0) {
        __ load_method_handle_vmslots(O1_collect_count, G3_method_handle, O2_scratch);
        collect_count = O1_collect_count;
      } else {
        collect_count = collect_count_constant;
#ifdef ASSERT
        if (VerifyMethodHandles) {
          BLOCK_COMMENT("verify collect_count_constant {");
          __ load_method_handle_vmslots(O3_scratch, G3_method_handle, O2_scratch);
          Label L_count_ok;
          __ cmp(O3_scratch, collect_count_constant);
          __ br(Assembler::equal, false, Assembler::pt, L_count_ok);
          __ delayed()->nop();
          __ stop("bad vminfo in AMH.conv");
          __ BIND(L_count_ok);
          BLOCK_COMMENT("} verify collect_count_constant");
        }
#endif //ASSERT
      }

      // copy |collect| slots directly to TOS:
      push_arg_slots(_masm, O0_coll, collect_count, O2_scratch, O3_scratch);
      // Now pushed:  ... keep1 | collect | keep2 | RF... | collect |
      // O0_coll still points at the trailing edge of |collect| and leading edge of |keep2|

      // If necessary, adjust the saved arguments to make room for the eventual return value.
      // Normal adjustment:  ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
      // If retaining args:  ... keep1 | +dest+ |  collect  | keep2 | RF... | collect |
      // In the non-retaining case, this might move keep2 either up or down.
      // We don't have to copy the whole | RF... collect | complex,
      // but we must adjust RF.saved_args_base.
      // Also, from now on, we will forget about the original copy of |collect|.
      // If we are retaining it, we will treat it as part of |keep2|.
      // For clarity we will define |keep3| = |collect|keep2| or |keep2|.

      BLOCK_COMMENT("adjust trailing arguments {");
      // Compare the sizes of |+dest+| and |-collect-|, which are opposed opening and closing movements.
      int                open_count  = dest_count;
      RegisterOrConstant close_count = collect_count_constant;
      Register O1_close_count = O1_collect_count;
      if (retain_original_args) {
        close_count = constant(0);
      } else if (collect_count_constant == -1) {
        close_count = O1_collect_count;
      }

      // How many slots need moving?  This is simply dest_slot (0 => no |keep3|).
      RegisterOrConstant keep3_count;
      Register O2_keep3_count = O2_scratch;
      if (dest_slot_constant < 0) {
        extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O2_keep3_count);
        keep3_count = O2_keep3_count;
      } else  {
        keep3_count = dest_slot_constant;
#ifdef ASSERT
        if (VerifyMethodHandles && dest_slot_constant < 0) {
          BLOCK_COMMENT("verify dest_slot_constant {");
          extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O3_scratch);
          Label L_vminfo_ok;
          __ cmp(O3_scratch, dest_slot_constant);
          __ br(Assembler::equal, false, Assembler::pt, L_vminfo_ok);
          __ delayed()->nop();
          __ stop("bad vminfo in AMH.conv");
          __ BIND(L_vminfo_ok);
          BLOCK_COMMENT("} verify dest_slot_constant");
        }
#endif //ASSERT
      }

      // tasks remaining:
      bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
      bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
      bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());

      // Old and new argument locations (based at slot 0).
      // Net shift (&new_argv - &old_argv) is (close_count - open_count).
      bool zero_open_count = (open_count == 0);  // remember this bit of info
      if (move_keep3 && fix_arg_base) {
        // It will be easier to have everything in one register:
        if (close_count.is_register()) {
          // Deduct open_count from close_count register to get a clean +/- value.
          __ sub(close_count.as_register(), open_count, close_count.as_register());
        } else {
          close_count = close_count.as_constant() - open_count;
        }
        open_count = 0;
      }
      Register L4_old_argv = RicochetFrame::L4_saved_args_base;
      Register O3_new_argv = O3_scratch;
      if (fix_arg_base) {
        __ add(L4_old_argv, __ argument_offset(close_count, O4_scratch), O3_new_argv,
               -(open_count * Interpreter::stackElementSize));
      }

      // First decide if any actual data are to be moved.
      // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
      // (As it happens, all movements involve an argument list size change.)

      // If there are variable parameters, use dynamic checks to skip around the whole mess.
      Label L_done;
      if (keep3_count.is_register()) {
        __ tst(keep3_count.as_register());
        __ br(Assembler::zero, false, Assembler::pn, L_done);
        __ delayed()->nop();
      }
      if (close_count.is_register()) {
        __ cmp(close_count.as_register(), open_count);
        __ br(Assembler::equal, false, Assembler::pn, L_done);
        __ delayed()->nop();
      }

      if (move_keep3 && fix_arg_base) {
        bool emit_move_down = false, emit_move_up = false, emit_guard = false;
        if (!close_count.is_constant()) {
          emit_move_down = emit_guard = !zero_open_count;
          emit_move_up   = true;
        } else if (open_count != close_count.as_constant()) {
          emit_move_down = (open_count > close_count.as_constant());
          emit_move_up   = !emit_move_down;
        }
        Label L_move_up;
        if (emit_guard) {
          __ cmp(close_count.as_register(), open_count);
          __ br(Assembler::greater, false, Assembler::pn, L_move_up);
          __ delayed()->nop();
        }

        if (emit_move_down) {
          // Move arguments down if |+dest+| > |-collect-|
          // (This is rare, except when arguments are retained.)
          // This opens space for the return value.
          if (keep3_count.is_constant()) {
            for (int i = 0; i < keep3_count.as_constant(); i++) {
              __ ld_ptr(            Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
              __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize)            );
            }
          } else {
            // Live: O1_close_count, O2_keep3_count, O3_new_argv
            Register argv_top = O0_scratch;
            __ add(L4_old_argv, __ argument_offset(keep3_count, O4_scratch), argv_top);
            move_arg_slots_down(_masm,
                                Address(L4_old_argv, 0),  // beginning of old argv
                                argv_top,                 // end of old argv
                                close_count,              // distance to move down (must be negative)
                                O4_scratch, G5_scratch);
          }
        }

        if (emit_guard) {
          __ ba(false, L_done);  // assumes emit_move_up is true also
          __ delayed()->nop();
          __ BIND(L_move_up);
        }

        if (emit_move_up) {
          // Move arguments up if |+dest+| < |-collect-|
          // (This is usual, except when |keep3| is empty.)
          // This closes up the space occupied by the now-deleted collect values.
          if (keep3_count.is_constant()) {
            for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
              __ ld_ptr(            Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
              __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize)            );
            }
          } else {
            Address argv_top(L4_old_argv, __ argument_offset(keep3_count, O4_scratch));
            // Live: O1_close_count, O2_keep3_count, O3_new_argv
            move_arg_slots_up(_masm,
                              L4_old_argv,  // beginning of old argv
                              argv_top,     // end of old argv
                              close_count,  // distance to move up (must be positive)
                              O4_scratch, G5_scratch);
          }
        }
      }
      __ BIND(L_done);

      if (fix_arg_base) {
        // adjust RF.saved_args_base
        __ mov(O3_new_argv, RicochetFrame::L4_saved_args_base);
      }

      if (stomp_dest) {
        // Stomp the return slot, so it doesn't hold garbage.
        // This isn't strictly necessary, but it may help detect bugs.
        __ set(RicochetFrame::RETURN_VALUE_PLACEHOLDER, O4_scratch);
        __ st_ptr(O4_scratch, Address(RicochetFrame::L4_saved_args_base,
                                      __ argument_offset(keep3_count, keep3_count.register_or_noreg())));  // uses O2_keep3_count
      }
      BLOCK_COMMENT("} adjust trailing arguments");

      BLOCK_COMMENT("do_recursive_call");
      __ mov(SP, O5_savedSP);  // record SP for the callee
      __ set(ExternalAddress(SharedRuntime::ricochet_blob()->bounce_addr() - frame::pc_return_offset), O7);
      // The globally unique bounce address has two purposes:
      // 1. It helps the JVM recognize this frame (frame::is_ricochet_frame).
      // 2. When returned to, it cuts back the stack and redirects control flow
      //    to the return handler.
      // The return handler will further cut back the stack when it takes
      // down the RF.  Perhaps there is a way to streamline this further.

      // State during recursive call:
      // ... keep1 | dest | dest=42 | keep3 | RF... | collect | bounce_pc |
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
T
twisti 已提交
2056 2057 2058
    }
    break;

2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
  case _adapter_opt_return_ref:
  case _adapter_opt_return_int:
  case _adapter_opt_return_long:
  case _adapter_opt_return_float:
  case _adapter_opt_return_double:
  case _adapter_opt_return_void:
  case _adapter_opt_return_S0_ref:
  case _adapter_opt_return_S1_ref:
  case _adapter_opt_return_S2_ref:
  case _adapter_opt_return_S3_ref:
  case _adapter_opt_return_S4_ref:
  case _adapter_opt_return_S5_ref:
    {
      BasicType dest_type_constant = ek_adapter_opt_return_type(ek);
      int       dest_slot_constant = ek_adapter_opt_return_slot(ek);

      if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);

      if (dest_slot_constant == -1) {
        // The current stub is a general handler for this dest_type.
        // It can be called from _adapter_opt_return_any below.
        // Stash the address in a little table.
        assert((dest_type_constant & CONV_TYPE_MASK) == dest_type_constant, "oob");
        address return_handler = __ pc();
        _adapter_return_handlers[dest_type_constant] = return_handler;
        if (dest_type_constant == T_INT) {
          // do the subword types too
          for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
            if (is_subword_type(BasicType(bt)) &&
                _adapter_return_handlers[bt] == NULL) {
              _adapter_return_handlers[bt] = return_handler;
            }
          }
        }
      }

      // On entry to this continuation handler, make Gargs live again.
      __ mov(RicochetFrame::L4_saved_args_base, Gargs);

      Register O7_temp   = O7;
      Register O5_vminfo = O5;

      RegisterOrConstant dest_slot = dest_slot_constant;
      if (dest_slot_constant == -1) {
        extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O5_vminfo);
        dest_slot = O5_vminfo;
      }
      // Store the result back into the argslot.
      // This code uses the interpreter calling sequence, in which the return value
      // is usually left in the TOS register, as defined by InterpreterMacroAssembler::pop.
      // There are certain irregularities with floating point values, which can be seen
      // in TemplateInterpreterGenerator::generate_return_entry_for.
      move_return_value(_masm, dest_type_constant, __ argument_address(dest_slot, O7_temp));

      RicochetFrame::leave_ricochet_frame(_masm, G3_method_handle, I5_savedSP, I7);

      // Load the final target and go.
      if (VerifyMethodHandles)  verify_method_handle(_masm, G3_method_handle, O0_scratch, O1_scratch);
      __ restore(I5_savedSP, G0, SP);
      __ jump_to_method_handle_entry(G3_method_handle, O0_scratch);
      __ illtrap(0);
    }
    break;

  case _adapter_opt_return_any:
    {
      Register O7_temp      = O7;
      Register O5_dest_type = O5;

      if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);
      extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O5_dest_type);
      __ set(ExternalAddress((address) &_adapter_return_handlers[0]), O7_temp);
      __ sll_ptr(O5_dest_type, LogBytesPerWord, O5_dest_type);
      __ ld_ptr(O7_temp, O5_dest_type, O7_temp);

#ifdef ASSERT
      { Label L_ok;
        __ br_notnull(O7_temp, false, Assembler::pt, L_ok);
        __ delayed()->nop();
        __ stop("bad method handle return");
        __ BIND(L_ok);
      }
#endif //ASSERT
      __ JMP(O7_temp, 0);
      __ delayed()->nop();
    }
    break;

  case _adapter_opt_spread_0:
  case _adapter_opt_spread_1_ref:
  case _adapter_opt_spread_2_ref:
  case _adapter_opt_spread_3_ref:
  case _adapter_opt_spread_4_ref:
  case _adapter_opt_spread_5_ref:
  case _adapter_opt_spread_ref:
  case _adapter_opt_spread_byte:
  case _adapter_opt_spread_char:
  case _adapter_opt_spread_short:
  case _adapter_opt_spread_int:
  case _adapter_opt_spread_long:
  case _adapter_opt_spread_float:
  case _adapter_opt_spread_double:
    {
      // spread an array out into a group of arguments
      int  length_constant    = ek_adapter_opt_spread_count(ek);
      bool length_can_be_zero = (length_constant == 0);
      if (length_constant < 0) {
        // some adapters with variable length must handle the zero case
        if (!OptimizeMethodHandles ||
            ek_adapter_opt_spread_type(ek) != T_OBJECT)
          length_can_be_zero = true;
      }

      // find the address of the array argument
      load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
      __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);

      // O0_argslot points both to the array and to the first output arg
      Address vmarg = Address(O0_argslot, 0);

      // Get the array value.
      Register  O1_array       = O1_scratch;
      Register  O2_array_klass = O2_scratch;
      BasicType elem_type      = ek_adapter_opt_spread_type(ek);
      int       elem_slots     = type2size[elem_type];  // 1 or 2
      int       array_slots    = 1;  // array is always a T_OBJECT
      int       length_offset  = arrayOopDesc::length_offset_in_bytes();
      int       elem0_offset   = arrayOopDesc::base_offset_in_bytes(elem_type);
      __ ld_ptr(vmarg, O1_array);

      Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
      if (length_can_be_zero) {
        // handle the null pointer case, if zero is allowed
        Label L_skip;
        if (length_constant < 0) {
          load_conversion_vminfo(_masm, G3_amh_conversion, O3_scratch);
          __ br_zero(Assembler::notZero, false, Assembler::pn, O3_scratch, L_skip);
          __ delayed()->nop();
        }
        __ br_null(O1_array, false, Assembler::pn, L_array_is_empty);
        __ delayed()->nop();
        __ BIND(L_skip);
      }
      __ null_check(O1_array, oopDesc::klass_offset_in_bytes());
      __ load_klass(O1_array, O2_array_klass);

      // Check the array type.
      Register O3_klass = O3_scratch;
      __ load_heap_oop(G3_amh_argument, O3_klass);  // this is a Class object!
      load_klass_from_Class(_masm, O3_klass, O4_scratch, G5_scratch);

      Label L_ok_array_klass, L_bad_array_klass, L_bad_array_length;
      __ check_klass_subtype(O2_array_klass, O3_klass, O4_scratch, G5_scratch, L_ok_array_klass);
      // If we get here, the type check failed!
      __ ba(false, L_bad_array_klass);
      __ delayed()->nop();
      __ BIND(L_ok_array_klass);

      // Check length.
      if (length_constant >= 0) {
        __ ldsw(Address(O1_array, length_offset), O4_scratch);
        __ cmp(O4_scratch, length_constant);
      } else {
        Register O3_vminfo = O3_scratch;
        load_conversion_vminfo(_masm, G3_amh_conversion, O3_vminfo);
        __ ldsw(Address(O1_array, length_offset), O4_scratch);
        __ cmp(O3_vminfo, O4_scratch);
      }
      __ br(Assembler::notEqual, false, Assembler::pn, L_bad_array_length);
      __ delayed()->nop();

      Register O2_argslot_limit = O2_scratch;

      // Array length checks out.  Now insert any required stack slots.
      if (length_constant == -1) {
        // Form a pointer to the end of the affected region.
        __ add(O0_argslot, Interpreter::stackElementSize, O2_argslot_limit);
        // 'stack_move' is negative number of words to insert
        // This number already accounts for elem_slots.
        Register O3_stack_move = O3_scratch;
        load_stack_move(_masm, G3_amh_conversion, O3_stack_move);
        __ cmp(O3_stack_move, 0);
        assert(stack_move_unit() < 0, "else change this comparison");
        __ br(Assembler::less, false, Assembler::pn, L_insert_arg_space);
        __ delayed()->nop();
        __ br(Assembler::equal, false, Assembler::pn, L_copy_args);
        __ delayed()->nop();
        // single argument case, with no array movement
        __ BIND(L_array_is_empty);
        remove_arg_slots(_masm, -stack_move_unit() * array_slots,
                         O0_argslot, O1_scratch, O2_scratch, O3_scratch);
        __ ba(false, L_args_done);  // no spreading to do
        __ delayed()->nop();
        __ BIND(L_insert_arg_space);
        // come here in the usual case, stack_move < 0 (2 or more spread arguments)
        // Live: O1_array, O2_argslot_limit, O3_stack_move
        insert_arg_slots(_masm, O3_stack_move,
                         O0_argslot, O4_scratch, G5_scratch, O1_scratch);
        // reload from rdx_argslot_limit since rax_argslot is now decremented
        __ ld_ptr(Address(O2_argslot_limit, -Interpreter::stackElementSize), O1_array);
      } else if (length_constant >= 1) {
        int new_slots = (length_constant * elem_slots) - array_slots;
        insert_arg_slots(_masm, new_slots * stack_move_unit(),
                         O0_argslot, O2_scratch, O3_scratch, O4_scratch);
      } else if (length_constant == 0) {
        __ BIND(L_array_is_empty);
        remove_arg_slots(_masm, -stack_move_unit() * array_slots,
                         O0_argslot, O1_scratch, O2_scratch, O3_scratch);
      } else {
        ShouldNotReachHere();
      }

      // Copy from the array to the new slots.
      // Note: Stack change code preserves integrity of O0_argslot pointer.
      // So even after slot insertions, O0_argslot still points to first argument.
      // Beware:  Arguments that are shallow on the stack are deep in the array,
      // and vice versa.  So a downward-growing stack (the usual) has to be copied
      // elementwise in reverse order from the source array.
      __ BIND(L_copy_args);
      if (length_constant == -1) {
        // [O0_argslot, O2_argslot_limit) is the area we are inserting into.
        // Array element [0] goes at O0_argslot_limit[-wordSize].
        Register O1_source = O1_array;
        __ add(Address(O1_array, elem0_offset), O1_source);
        Register O4_fill_ptr = O4_scratch;
        __ mov(O2_argslot_limit, O4_fill_ptr);
        Label L_loop;
        __ BIND(L_loop);
        __ add(O4_fill_ptr, -Interpreter::stackElementSize * elem_slots, O4_fill_ptr);
        move_typed_arg(_masm, elem_type, true,
                       Address(O1_source, 0), Address(O4_fill_ptr, 0),
                       O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
        __ add(O1_source, type2aelembytes(elem_type), O1_source);
        __ cmp(O4_fill_ptr, O0_argslot);
        __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
        __ delayed()->nop();  // FILLME
      } else if (length_constant == 0) {
        // nothing to copy
      } else {
        int elem_offset = elem0_offset;
        int slot_offset = length_constant * Interpreter::stackElementSize;
        for (int index = 0; index < length_constant; index++) {
          slot_offset -= Interpreter::stackElementSize * elem_slots;  // fill backward
          move_typed_arg(_masm, elem_type, true,
                         Address(O1_array, elem_offset), Address(O0_argslot, slot_offset),
                         O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
          elem_offset += type2aelembytes(elem_type);
        }
      }
      __ BIND(L_args_done);

      // Arguments are spread.  Move to next method handle.
      __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
      __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);

      __ BIND(L_bad_array_klass);
      assert(!vmarg.uses(O2_required), "must be different registers");
      __ load_heap_oop(Address(O2_array_klass, java_mirror_offset), O2_required);  // required class
      __ ld_ptr(       vmarg,                                       O1_actual);    // bad object
      __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
      __ delayed()->mov(Bytecodes::_aaload,                         O0_code);      // who is complaining?

      __ bind(L_bad_array_length);
      assert(!vmarg.uses(O2_required), "must be different registers");
      __ mov(   G3_method_handle,                O2_required);  // required class
      __ ld_ptr(vmarg,                           O1_actual);    // bad object
      __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
      __ delayed()->mov(Bytecodes::_arraylength, O0_code);      // who is complaining?
    }
T
twisti 已提交
2328 2329 2330
    break;

  default:
2331
    DEBUG_ONLY(tty->print_cr("bad ek=%d (%s)", (int)ek, entry_name(ek)));
T
twisti 已提交
2332 2333
    ShouldNotReachHere();
  }
2334
  BLOCK_COMMENT(err_msg("} Entry %s", entry_name(ek)));
T
twisti 已提交
2335 2336 2337 2338 2339

  address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
  __ unimplemented(entry_name(ek)); // %%% FIXME: NYI

  init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
2340
}