1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "interpreter/interpreter.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "prims/methodHandles.hpp"
  29 
  30 #define __ _masm->
  31 
  32 #ifdef PRODUCT
  33 #define BLOCK_COMMENT(str) /* nothing */
  34 #else
  35 #define BLOCK_COMMENT(str) __ block_comment(str)
  36 #endif
  37 
  38 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  39 
  40 address MethodHandleEntry::start_compiled_entry(MacroAssembler* _masm,
  41                                                 address interpreted_entry) {
  42   // Just before the actual machine code entry point, allocate space
  43   // for a MethodHandleEntry::Data record, so that we can manage everything
  44   // from one base pointer.
  45   __ align(wordSize);
  46   address target = __ pc() + sizeof(Data);
  47   while (__ pc() < target) {
  48     __ nop();
  49     __ align(wordSize);
  50   }
  51 
  52   MethodHandleEntry* me = (MethodHandleEntry*) __ pc();
  53   me->set_end_address(__ pc());         // set a temporary end_address
  54   me->set_from_interpreted_entry(interpreted_entry);
  55   me->set_type_checking_entry(NULL);
  56 
  57   return (address) me;
  58 }
  59 
  60 MethodHandleEntry* MethodHandleEntry::finish_compiled_entry(MacroAssembler* _masm,
  61                                                 address start_addr) {
  62   MethodHandleEntry* me = (MethodHandleEntry*) start_addr;
  63   assert(me->end_address() == start_addr, "valid ME");
  64 
  65   // Fill in the real end_address:
  66   __ align(wordSize);
  67   me->set_end_address(__ pc());
  68 
  69   return me;
  70 }
  71 
  72 // stack walking support
  73 
  74 frame MethodHandles::ricochet_frame_sender(const frame& fr, RegisterMap *map) {
  75   //RicochetFrame* f = RicochetFrame::from_frame(fr);
  76   // Cf. is_interpreted_frame path of frame::sender
  77   intptr_t* younger_sp = fr.sp();
  78   intptr_t* sp         = fr.sender_sp();
  79   map->make_integer_regs_unsaved();
  80   map->shift_window(sp, younger_sp);
  81   bool this_frame_adjusted_stack = true;  // I5_savedSP is live in this RF
  82   return frame(sp, younger_sp, this_frame_adjusted_stack);
  83 }
  84 
  85 void MethodHandles::ricochet_frame_oops_do(const frame& fr, OopClosure* blk, const RegisterMap* reg_map) {
  86   ResourceMark rm;
  87   RicochetFrame* f = RicochetFrame::from_frame(fr);
  88 
  89   // pick up the argument type descriptor:
  90   Thread* thread = Thread::current();
  91   Handle cookie(thread, f->compute_saved_args_layout(true, true));
  92 
  93   // process fixed part
  94   blk->do_oop((oop*)f->saved_target_addr());
  95   blk->do_oop((oop*)f->saved_args_layout_addr());
  96 
  97   // process variable arguments:
  98   if (cookie.is_null())  return;  // no arguments to describe
  99 
 100   // the cookie is actually the invokeExact method for my target
 101   // his argument signature is what I'm interested in
 102   assert(cookie->is_method(), "");
 103   methodHandle invoker(thread, methodOop(cookie()));
 104   assert(invoker->name() == vmSymbols::invokeExact_name(), "must be this kind of method");
 105   assert(!invoker->is_static(), "must have MH argument");
 106   int slot_count = invoker->size_of_parameters();
 107   assert(slot_count >= 1, "must include 'this'");
 108   intptr_t* base = f->saved_args_base();
 109   intptr_t* retval = NULL;
 110   if (f->has_return_value_slot())
 111     retval = f->return_value_slot_addr();
 112   int slot_num = slot_count - 1;
 113   intptr_t* loc = &base[slot_num];
 114   //blk->do_oop((oop*) loc);   // original target, which is irrelevant
 115   int arg_num = 0;
 116   for (SignatureStream ss(invoker->signature()); !ss.is_done(); ss.next()) {
 117     if (ss.at_return_type())  continue;
 118     BasicType ptype = ss.type();
 119     if (ptype == T_ARRAY)  ptype = T_OBJECT; // fold all refs to T_OBJECT
 120     assert(ptype >= T_BOOLEAN && ptype <= T_OBJECT, "not array or void");
 121     slot_num -= type2size[ptype];
 122     loc = &base[slot_num];
 123     bool is_oop = (ptype == T_OBJECT && loc != retval);
 124     if (is_oop)  blk->do_oop((oop*)loc);
 125     arg_num += 1;
 126   }
 127   assert(slot_num == 0, "must have processed all the arguments");
 128 }
 129 
 130 // Ricochet Frames
 131 const Register MethodHandles::RicochetFrame::L1_continuation      = L1;
 132 const Register MethodHandles::RicochetFrame::L2_saved_target      = L2;
 133 const Register MethodHandles::RicochetFrame::L3_saved_args_layout = L3;
 134 const Register MethodHandles::RicochetFrame::L4_saved_args_base   = L4; // cf. Gargs = G4
 135 const Register MethodHandles::RicochetFrame::L5_conversion        = L5;
 136 #ifdef ASSERT
 137 const Register MethodHandles::RicochetFrame::L0_magic_number_1    = L0;
 138 #endif //ASSERT
 139 
 140 oop MethodHandles::RicochetFrame::compute_saved_args_layout(bool read_cache, bool write_cache) {
 141   if (read_cache) {
 142     oop cookie = saved_args_layout();
 143     if (cookie != NULL)  return cookie;
 144   }
 145   oop target = saved_target();
 146   oop mtype  = java_lang_invoke_MethodHandle::type(target);
 147   oop mtform = java_lang_invoke_MethodType::form(mtype);
 148   oop cookie = java_lang_invoke_MethodTypeForm::vmlayout(mtform);
 149   if (write_cache)  {
 150     (*saved_args_layout_addr()) = cookie;
 151   }
 152   return cookie;
 153 }
 154 
 155 void MethodHandles::RicochetFrame::generate_ricochet_blob(MacroAssembler* _masm,
 156                                                           // output params:
 157                                                           int* bounce_offset,
 158                                                           int* exception_offset,
 159                                                           int* frame_size_in_words) {
 160   (*frame_size_in_words) = RicochetFrame::frame_size_in_bytes() / wordSize;
 161 
 162   address start = __ pc();
 163 
 164 #ifdef ASSERT
 165   __ illtrap(0); __ illtrap(0); __ illtrap(0);
 166   // here's a hint of something special:
 167   __ set(MAGIC_NUMBER_1, G0);
 168   __ set(MAGIC_NUMBER_2, G0);
 169 #endif //ASSERT
 170   __ illtrap(0);  // not reached
 171 
 172   // Return values are in registers.
 173   // L1_continuation contains a cleanup continuation we must return
 174   // to.
 175 
 176   (*bounce_offset) = __ pc() - start;
 177   BLOCK_COMMENT("ricochet_blob.bounce");
 178 
 179   if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);
 180   trace_method_handle(_masm, "ricochet_blob.bounce");
 181 
 182   __ JMP(L1_continuation, 0);
 183   __ delayed()->nop();
 184   __ illtrap(0);
 185 
 186   DEBUG_ONLY(__ set(MAGIC_NUMBER_2, G0));
 187 
 188   (*exception_offset) = __ pc() - start;
 189   BLOCK_COMMENT("ricochet_blob.exception");
 190 
 191   // compare this to Interpreter::rethrow_exception_entry, which is parallel code
 192   // for example, see TemplateInterpreterGenerator::generate_throw_exception
 193   // Live registers in:
 194   //   Oexception  (O0): exception
 195   //   Oissuing_pc (O1): return address/pc that threw exception (ignored, always equal to bounce addr)
 196   __ verify_oop(Oexception);
 197 
 198   // Take down the frame.
 199 
 200   // Cf. InterpreterMacroAssembler::remove_activation.
 201   leave_ricochet_frame(_masm, /*recv_reg=*/ noreg, I5_savedSP, I7);
 202 
 203   // We are done with this activation frame; find out where to go next.
 204   // The continuation point will be an exception handler, which expects
 205   // the following registers set up:
 206   //
 207   // Oexception: exception
 208   // Oissuing_pc: the local call that threw exception
 209   // Other On: garbage
 210   // In/Ln:  the contents of the caller's register window
 211   //
 212   // We do the required restore at the last possible moment, because we
 213   // need to preserve some state across a runtime call.
 214   // (Remember that the caller activation is unknown--it might not be
 215   // interpreted, so things like Lscratch are useless in the caller.)
 216   __ mov(Oexception,  Oexception ->after_save());  // get exception in I0 so it will be on O0 after restore
 217   __ add(I7, frame::pc_return_offset, Oissuing_pc->after_save());  // likewise set I1 to a value local to the caller
 218   __ call_VM_leaf(L7_thread_cache,
 219                   CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
 220                   G2_thread, Oissuing_pc->after_save());
 221 
 222   // The caller's SP was adjusted upon method entry to accomodate
 223   // the callee's non-argument locals. Undo that adjustment.
 224   __ JMP(O0, 0);                         // return exception handler in caller
 225   __ delayed()->restore(I5_savedSP, G0, SP);
 226 
 227   // (same old exception object is already in Oexception; see above)
 228   // Note that an "issuing PC" is actually the next PC after the call
 229 }
 230 
 231 void MethodHandles::RicochetFrame::enter_ricochet_frame(MacroAssembler* _masm,
 232                                                         Register recv_reg,
 233                                                         Register argv_reg,
 234                                                         address return_handler) {
 235   // does not include the __ save()
 236   assert(argv_reg == Gargs, "");
 237   Address G3_mh_vmtarget(   recv_reg, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
 238   Address G3_amh_conversion(recv_reg, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
 239 
 240   // Create the RicochetFrame.
 241   // Unlike on x86 we can store all required information in local
 242   // registers.
 243   BLOCK_COMMENT("push RicochetFrame {");
 244   __ set(ExternalAddress(return_handler),          L1_continuation);
 245   __ load_heap_oop(G3_mh_vmtarget,                 L2_saved_target);
 246   __ mov(G0,                                       L3_saved_args_layout);
 247   __ mov(Gargs,                                    L4_saved_args_base);
 248   __ lduw(G3_amh_conversion,                       L5_conversion);  // 32-bit field
 249   // I5, I6, I7 are already set up
 250   DEBUG_ONLY(__ set((int32_t) MAGIC_NUMBER_1,      L0_magic_number_1));
 251   BLOCK_COMMENT("} RicochetFrame");
 252 }
 253 
 254 void MethodHandles::RicochetFrame::leave_ricochet_frame(MacroAssembler* _masm,
 255                                                         Register recv_reg,
 256                                                         Register new_sp_reg,
 257                                                         Register sender_pc_reg) {
 258   assert(new_sp_reg == I5_savedSP, "exact_sender_sp already in place");
 259   assert(sender_pc_reg == I7, "in a fixed place");
 260   // does not include the __ ret() & __ restore()
 261   assert_different_registers(recv_reg, new_sp_reg, sender_pc_reg);
 262   // Take down the frame.
 263   // Cf. InterpreterMacroAssembler::remove_activation.
 264   BLOCK_COMMENT("end_ricochet_frame {");
 265   if (recv_reg->is_valid())
 266     __ mov(L2_saved_target, recv_reg);
 267   BLOCK_COMMENT("} end_ricochet_frame");
 268 }
 269 
 270 // Emit code to verify that FP is pointing at a valid ricochet frame.
 271 #ifdef ASSERT
 272 enum {
 273   ARG_LIMIT = 255, SLOP = 45,
 274   // use this parameter for checking for garbage stack movements:
 275   UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
 276   // the slop defends against false alarms due to fencepost errors
 277 };
 278 
 279 void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
 280   // The stack should look like this:
 281   //    ... keep1 | dest=42 | keep2 | magic | handler | magic | recursive args | [RF]
 282   // Check various invariants.
 283 
 284   Register O7_temp = O7, O5_temp = O5;
 285 
 286   Label L_ok_1, L_ok_2, L_ok_3, L_ok_4;
 287   BLOCK_COMMENT("verify_clean {");
 288   // Magic numbers must check out:
 289   __ set((int32_t) MAGIC_NUMBER_1, O7_temp);
 290   __ cmp_and_br_short(O7_temp, L0_magic_number_1, Assembler::equal, Assembler::pt, L_ok_1);
 291   __ stop("damaged ricochet frame: MAGIC_NUMBER_1 not found");
 292 
 293   __ BIND(L_ok_1);
 294 
 295   // Arguments pointer must look reasonable:
 296 #ifdef _LP64
 297   Register FP_temp = O5_temp;
 298   __ add(FP, STACK_BIAS, FP_temp);
 299 #else
 300   Register FP_temp = FP;
 301 #endif
 302   __ cmp_and_brx_short(L4_saved_args_base, FP_temp, Assembler::greaterEqualUnsigned, Assembler::pt, L_ok_2);
 303   __ stop("damaged ricochet frame: L4 < FP");
 304 
 305   __ BIND(L_ok_2);
 306   // Disable until we decide on it's fate
 307   // __ sub(L4_saved_args_base, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize, O7_temp);
 308   // __ cmp(O7_temp, FP_temp);
 309   // __ br(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok_3);
 310   // __ delayed()->nop();
 311   // __ stop("damaged ricochet frame: (L4 - UNREASONABLE_STACK_MOVE) > FP");
 312 
 313   __ BIND(L_ok_3);
 314   extract_conversion_dest_type(_masm, L5_conversion, O7_temp);
 315   __ cmp_and_br_short(O7_temp, T_VOID, Assembler::equal, Assembler::pt, L_ok_4);
 316   extract_conversion_vminfo(_masm, L5_conversion, O5_temp);
 317   __ ld_ptr(L4_saved_args_base, __ argument_offset(O5_temp, O5_temp), O7_temp);
 318   assert(__ is_simm13(RETURN_VALUE_PLACEHOLDER), "must be simm13");
 319   __ cmp_and_brx_short(O7_temp, (int32_t) RETURN_VALUE_PLACEHOLDER, Assembler::equal, Assembler::pt, L_ok_4);
 320   __ stop("damaged ricochet frame: RETURN_VALUE_PLACEHOLDER not found");
 321   __ BIND(L_ok_4);
 322   BLOCK_COMMENT("} verify_clean");
 323 }
 324 #endif //ASSERT
 325 
 326 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
 327   if (VerifyMethodHandles)
 328     verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(), temp_reg, temp2_reg,
 329                  "AMH argument is a Class");
 330   __ load_heap_oop(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
 331 }
 332 
 333 void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Address conversion_field_addr, Register reg) {
 334   assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 335   assert(CONV_VMINFO_MASK == right_n_bits(BitsPerByte), "else change type of following load");
 336   __ ldub(conversion_field_addr.plus_disp(BytesPerInt - 1), reg);
 337 }
 338 
 339 void MethodHandles::extract_conversion_vminfo(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
 340   assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 341   __ and3(conversion_field_reg, CONV_VMINFO_MASK, reg);
 342 }
 343 
 344 void MethodHandles::extract_conversion_dest_type(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
 345   __ srl(conversion_field_reg, CONV_DEST_TYPE_SHIFT, reg);
 346   __ and3(reg, 0x0F, reg);
 347 }
 348 
 349 void MethodHandles::load_stack_move(MacroAssembler* _masm,
 350                                     Address G3_amh_conversion,
 351                                     Register stack_move_reg) {
 352   BLOCK_COMMENT("load_stack_move {");
 353   __ ldsw(G3_amh_conversion, stack_move_reg);
 354   __ sra(stack_move_reg, CONV_STACK_MOVE_SHIFT, stack_move_reg);
 355   if (VerifyMethodHandles) {
 356     Label L_ok, L_bad;
 357     int32_t stack_move_limit = 0x0800;  // extra-large
 358     __ cmp_and_br_short(stack_move_reg, stack_move_limit, Assembler::greaterEqual, Assembler::pn, L_bad);
 359     __ cmp(stack_move_reg, -stack_move_limit);
 360     __ br(Assembler::greater, false, Assembler::pt, L_ok);
 361     __ delayed()->nop();
 362     __ BIND(L_bad);
 363     __ stop("load_stack_move of garbage value");
 364     __ BIND(L_ok);
 365   }
 366   BLOCK_COMMENT("} load_stack_move");
 367 }
 368 
 369 #ifdef ASSERT
 370 void MethodHandles::RicochetFrame::verify() const {
 371   assert(magic_number_1() == MAGIC_NUMBER_1, "");
 372   if (!Universe::heap()->is_gc_active()) {
 373     if (saved_args_layout() != NULL) {
 374       assert(saved_args_layout()->is_method(), "must be valid oop");
 375     }
 376     if (saved_target() != NULL) {
 377       assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
 378     }
 379   }
 380   int conv_op = adapter_conversion_op(conversion());
 381   assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
 382          conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
 383          conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
 384          "must be a sane conversion");
 385   if (has_return_value_slot()) {
 386     assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
 387   }
 388 }
 389 
 390 void MethodHandles::verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
 391   // Verify that argslot lies within (Gargs, FP].
 392   Label L_ok, L_bad;
 393   BLOCK_COMMENT("verify_argslot {");
 394   __ cmp_and_brx_short(Gargs, argslot_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
 395   __ add(FP, STACK_BIAS, temp_reg);  // STACK_BIAS is zero on !_LP64
 396   __ cmp_and_brx_short(argslot_reg, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
 397   __ BIND(L_bad);
 398   __ stop(error_message);
 399   __ BIND(L_ok);
 400   BLOCK_COMMENT("} verify_argslot");
 401 }
 402 
 403 void MethodHandles::verify_argslots(MacroAssembler* _masm,
 404                                     RegisterOrConstant arg_slots,
 405                                     Register arg_slot_base_reg,
 406                                     Register temp_reg,
 407                                     Register temp2_reg,
 408                                     bool negate_argslots,
 409                                     const char* error_message) {
 410   // Verify that [argslot..argslot+size) lies within (Gargs, FP).
 411   Label L_ok, L_bad;
 412   BLOCK_COMMENT("verify_argslots {");
 413   if (negate_argslots) {
 414     if (arg_slots.is_constant()) {
 415       arg_slots = -1 * arg_slots.as_constant();
 416     } else {
 417       __ neg(arg_slots.as_register(), temp_reg);
 418       arg_slots = temp_reg;
 419     }
 420   }
 421   __ add(arg_slot_base_reg, __ argument_offset(arg_slots, temp_reg), temp_reg);
 422   __ add(FP, STACK_BIAS, temp2_reg);  // STACK_BIAS is zero on !_LP64
 423   __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
 424   // Gargs points to the first word so adjust by BytesPerWord
 425   __ add(arg_slot_base_reg, BytesPerWord, temp_reg);
 426   __ cmp_and_brx_short(Gargs, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
 427   __ BIND(L_bad);
 428   __ stop(error_message);
 429   __ BIND(L_ok);
 430   BLOCK_COMMENT("} verify_argslots");
 431 }
 432 
 433 // Make sure that arg_slots has the same sign as the given direction.
 434 // If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
 435 void MethodHandles::verify_stack_move(MacroAssembler* _masm,
 436                                       RegisterOrConstant arg_slots, int direction) {
 437   enum { UNREASONABLE_STACK_MOVE = 256 * 4 };  // limit of 255 arguments
 438   bool allow_zero = arg_slots.is_constant();
 439   if (direction == 0) { direction = +1; allow_zero = true; }
 440   assert(stack_move_unit() == -1, "else add extra checks here");
 441   if (arg_slots.is_register()) {
 442     Label L_ok, L_bad;
 443     BLOCK_COMMENT("verify_stack_move {");
 444     // __ btst(-stack_move_unit() - 1, arg_slots.as_register());  // no need
 445     // __ br(Assembler::notZero, false, Assembler::pn, L_bad);
 446     // __ delayed()->nop();
 447     __ cmp(arg_slots.as_register(), (int32_t) NULL_WORD);
 448     if (direction > 0) {
 449       __ br(allow_zero ? Assembler::less : Assembler::lessEqual, false, Assembler::pn, L_bad);
 450       __ delayed()->nop();
 451       __ cmp(arg_slots.as_register(), (int32_t) UNREASONABLE_STACK_MOVE);
 452       __ br(Assembler::less, false, Assembler::pn, L_ok);
 453       __ delayed()->nop();
 454     } else {
 455       __ br(allow_zero ? Assembler::greater : Assembler::greaterEqual, false, Assembler::pn, L_bad);
 456       __ delayed()->nop();
 457       __ cmp(arg_slots.as_register(), (int32_t) -UNREASONABLE_STACK_MOVE);
 458       __ br(Assembler::greater, false, Assembler::pn, L_ok);
 459       __ delayed()->nop();
 460     }
 461     __ BIND(L_bad);
 462     if (direction > 0)
 463       __ stop("assert arg_slots > 0");
 464     else
 465       __ stop("assert arg_slots < 0");
 466     __ BIND(L_ok);
 467     BLOCK_COMMENT("} verify_stack_move");
 468   } else {
 469     intptr_t size = arg_slots.as_constant();
 470     if (direction < 0)  size = -size;
 471     assert(size >= 0, "correct direction of constant move");
 472     assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
 473   }
 474 }
 475 
 476 void MethodHandles::verify_klass(MacroAssembler* _masm,
 477                                  Register obj_reg, KlassHandle klass,
 478                                  Register temp_reg, Register temp2_reg,
 479                                  const char* error_message) {
 480   oop* klass_addr = klass.raw_value();
 481   assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
 482          klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
 483          "must be one of the SystemDictionaryHandles");
 484   Label L_ok, L_bad;
 485   BLOCK_COMMENT("verify_klass {");
 486   __ verify_oop(obj_reg);
 487   __ br_null_short(obj_reg, Assembler::pn, L_bad);
 488   __ load_klass(obj_reg, temp_reg);
 489   __ set(ExternalAddress(klass_addr), temp2_reg);
 490   __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
 491   __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
 492   intptr_t super_check_offset = klass->super_check_offset();
 493   __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
 494   __ set(ExternalAddress(klass_addr), temp2_reg);
 495   __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
 496   __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
 497   __ BIND(L_bad);
 498   __ stop(error_message);
 499   __ BIND(L_ok);
 500   BLOCK_COMMENT("} verify_klass");
 501 }
 502 #endif // ASSERT
 503 
 504 
 505 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp) {
 506   assert(method == G5_method, "interpreter calling convention");
 507   __ verify_oop(method);
 508   __ ld_ptr(G5_method, in_bytes(methodOopDesc::from_interpreted_offset()), target);
 509   if (JvmtiExport::can_post_interpreter_events()) {
 510     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 511     // compiled code in threads for which the event is enabled.  Check here for
 512     // interp_only_mode if these events CAN be enabled.
 513     __ verify_thread();
 514     Label skip_compiled_code;
 515 
 516     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
 517     __ ld(interp_only, temp);
 518     __ tst(temp);
 519     __ br(Assembler::notZero, true, Assembler::pn, skip_compiled_code);
 520     __ delayed()->ld_ptr(G5_method, in_bytes(methodOopDesc::interpreter_entry_offset()), target);
 521     __ bind(skip_compiled_code);
 522   }
 523   __ jmp(target, 0);
 524   __ delayed()->nop();
 525 }
 526 
 527 
 528 // Code generation
 529 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
 530   // I5_savedSP/O5_savedSP: sender SP (must preserve)
 531   // G4 (Gargs): incoming argument list (must preserve)
 532   // G5_method:  invoke methodOop
 533   // G3_method_handle: receiver method handle (must load from sp[MethodTypeForm.vmslots])
 534   // O0, O1, O2, O3, O4: garbage temps, blown away
 535   Register O0_mtype   = O0;
 536   Register O1_scratch = O1;
 537   Register O2_scratch = O2;
 538   Register O3_scratch = O3;
 539   Register O4_argslot = O4;
 540   Register O4_argbase = O4;
 541 
 542   // emit WrongMethodType path first, to enable back-branch from main path
 543   Label wrong_method_type;
 544   __ bind(wrong_method_type);
 545   Label invoke_generic_slow_path;
 546   assert(methodOopDesc::intrinsic_id_size_in_bytes() == sizeof(u1), "");;
 547   __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
 548   __ cmp(O1_scratch, (int) vmIntrinsics::_invokeExact);
 549   __ brx(Assembler::notEqual, false, Assembler::pt, invoke_generic_slow_path);
 550   __ delayed()->nop();
 551   __ mov(O0_mtype, G5_method_type);  // required by throw_WrongMethodType
 552   __ mov(G3_method_handle, G3_method_handle);  // already in this register
 553   // O0 will be filled in with JavaThread in stub
 554   __ jump_to(AddressLiteral(StubRoutines::throw_WrongMethodTypeException_entry()), O3_scratch);
 555   __ delayed()->nop();
 556 
 557   // here's where control starts out:
 558   __ align(CodeEntryAlignment);
 559   address entry_point = __ pc();
 560 
 561   // fetch the MethodType from the method handle
 562   // FIXME: Interpreter should transmit pre-popped stack pointer, to locate base of arg list.
 563   // This would simplify several touchy bits of code.
 564   // See 6984712: JSR 292 method handle calls need a clean argument base pointer
 565   {
 566     Register tem = G5_method;
 567     for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
 568       __ ld_ptr(Address(tem, *pchase), O0_mtype);
 569       tem = O0_mtype;          // in case there is another indirection
 570     }
 571   }
 572 
 573   // given the MethodType, find out where the MH argument is buried
 574   __ load_heap_oop(Address(O0_mtype,   __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes,        O1_scratch)), O4_argslot);
 575   __ ldsw(         Address(O4_argslot, __ delayed_value(java_lang_invoke_MethodTypeForm::vmslots_offset_in_bytes, O1_scratch)), O4_argslot);
 576   __ add(__ argument_address(O4_argslot, O4_argslot, 1), O4_argbase);
 577   // Note: argument_address uses its input as a scratch register!
 578   Address mh_receiver_slot_addr(O4_argbase, -Interpreter::stackElementSize);
 579   __ ld_ptr(mh_receiver_slot_addr, G3_method_handle);
 580 
 581   trace_method_handle(_masm, "invokeExact");
 582 
 583   __ check_method_handle_type(O0_mtype, G3_method_handle, O1_scratch, wrong_method_type);
 584 
 585   // Nobody uses the MH receiver slot after this.  Make sure.
 586   DEBUG_ONLY(__ set((int32_t) 0x999999, O1_scratch); __ st_ptr(O1_scratch, mh_receiver_slot_addr));
 587 
 588   __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 589 
 590   // for invokeGeneric (only), apply argument and result conversions on the fly
 591   __ bind(invoke_generic_slow_path);
 592 #ifdef ASSERT
 593   if (VerifyMethodHandles) {
 594     Label L;
 595     __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
 596     __ cmp(O1_scratch, (int) vmIntrinsics::_invokeGeneric);
 597     __ brx(Assembler::equal, false, Assembler::pt, L);
 598     __ delayed()->nop();
 599     __ stop("bad methodOop::intrinsic_id");
 600     __ bind(L);
 601   }
 602 #endif //ASSERT
 603 
 604   // make room on the stack for another pointer:
 605   insert_arg_slots(_masm, 2 * stack_move_unit(), O4_argbase, O1_scratch, O2_scratch, O3_scratch);
 606   // load up an adapter from the calling type (Java weaves this)
 607   Register O2_form    = O2_scratch;
 608   Register O3_adapter = O3_scratch;
 609   __ load_heap_oop(Address(O0_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes,               O1_scratch)), O2_form);
 610   __ load_heap_oop(Address(O2_form,  __ delayed_value(java_lang_invoke_MethodTypeForm::genericInvoker_offset_in_bytes, O1_scratch)), O3_adapter);
 611   __ verify_oop(O3_adapter);
 612   __ st_ptr(O3_adapter, Address(O4_argbase, 1 * Interpreter::stackElementSize));
 613   // As a trusted first argument, pass the type being called, so the adapter knows
 614   // the actual types of the arguments and return values.
 615   // (Generic invokers are shared among form-families of method-type.)
 616   __ st_ptr(O0_mtype,   Address(O4_argbase, 0 * Interpreter::stackElementSize));
 617   // FIXME: assert that O3_adapter is of the right method-type.
 618   __ mov(O3_adapter, G3_method_handle);
 619   trace_method_handle(_masm, "invokeGeneric");
 620   __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 621 
 622   return entry_point;
 623 }
 624 
 625 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
 626 static RegisterOrConstant constant(int value) {
 627   return RegisterOrConstant(value);
 628 }
 629 
 630 static void load_vmargslot(MacroAssembler* _masm, Address vmargslot_addr, Register result) {
 631   __ ldsw(vmargslot_addr, result);
 632 }
 633 
 634 static RegisterOrConstant adjust_SP_and_Gargs_down_by_slots(MacroAssembler* _masm,
 635                                                             RegisterOrConstant arg_slots,
 636                                                             Register temp_reg, Register temp2_reg) {
 637   // Keep the stack pointer 2*wordSize aligned.
 638   const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
 639   if (arg_slots.is_constant()) {
 640     const int        offset = arg_slots.as_constant() << LogBytesPerWord;
 641     const int masked_offset = round_to(offset, 2 * BytesPerWord);
 642     const int masked_offset2 = (offset + 1*BytesPerWord) & ~TwoWordAlignmentMask;
 643     assert(masked_offset == masked_offset2, "must agree");
 644     __ sub(Gargs,        offset, Gargs);
 645     __ sub(SP,    masked_offset, SP   );
 646     return offset;
 647   } else {
 648 #ifdef ASSERT
 649     {
 650       Label L_ok;
 651       __ cmp_and_br_short(arg_slots.as_register(), 0, Assembler::greaterEqual, Assembler::pt, L_ok);
 652       __ stop("negative arg_slots");
 653       __ bind(L_ok);
 654     }
 655 #endif
 656     __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
 657     __ add( temp_reg,  1*BytesPerWord,       temp2_reg);
 658     __ andn(temp2_reg, TwoWordAlignmentMask, temp2_reg);
 659     __ sub(Gargs, temp_reg,  Gargs);
 660     __ sub(SP,    temp2_reg, SP   );
 661     return temp_reg;
 662   }
 663 }
 664 
 665 static RegisterOrConstant adjust_SP_and_Gargs_up_by_slots(MacroAssembler* _masm,
 666                                                           RegisterOrConstant arg_slots,
 667                                                           Register temp_reg, Register temp2_reg) {
 668   // Keep the stack pointer 2*wordSize aligned.
 669   const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
 670   if (arg_slots.is_constant()) {
 671     const int        offset = arg_slots.as_constant() << LogBytesPerWord;
 672     const int masked_offset = offset & ~TwoWordAlignmentMask;
 673     __ add(Gargs,        offset, Gargs);
 674     __ add(SP,    masked_offset, SP   );
 675     return offset;
 676   } else {
 677     __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
 678     __ andn(temp_reg, TwoWordAlignmentMask, temp2_reg);
 679     __ add(Gargs, temp_reg,  Gargs);
 680     __ add(SP,    temp2_reg, SP   );
 681     return temp_reg;
 682   }
 683 }
 684 
 685 // Helper to insert argument slots into the stack.
 686 // arg_slots must be a multiple of stack_move_unit() and < 0
 687 // argslot_reg is decremented to point to the new (shifted) location of the argslot
 688 // But, temp_reg ends up holding the original value of argslot_reg.
 689 void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
 690                                      RegisterOrConstant arg_slots,
 691                                      Register argslot_reg,
 692                                      Register temp_reg, Register temp2_reg, Register temp3_reg) {
 693   // allow constant zero
 694   if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
 695     return;
 696 
 697   assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
 698                              (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
 699 
 700   BLOCK_COMMENT("insert_arg_slots {");
 701   if (VerifyMethodHandles)
 702     verify_argslot(_masm, argslot_reg, temp_reg, "insertion point must fall within current frame");
 703   if (VerifyMethodHandles)
 704     verify_stack_move(_masm, arg_slots, -1);
 705 
 706   // Make space on the stack for the inserted argument(s).
 707   // Then pull down everything shallower than argslot_reg.
 708   // The stacked return address gets pulled down with everything else.
 709   // That is, copy [sp, argslot) downward by -size words.  In pseudo-code:
 710   //   sp -= size;
 711   //   for (temp = sp + size; temp < argslot; temp++)
 712   //     temp[-size] = temp[0]
 713   //   argslot -= size;
 714 
 715   // offset is temp3_reg in case of arg_slots being a register.
 716   RegisterOrConstant offset = adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
 717   __ sub(Gargs, offset, temp_reg);  // source pointer for copy
 718 
 719   {
 720     Label loop;
 721     __ BIND(loop);
 722     // pull one word down each time through the loop
 723     __ ld_ptr(           Address(temp_reg, 0     ), temp2_reg);
 724     __ st_ptr(temp2_reg, Address(temp_reg, offset)           );
 725     __ add(temp_reg, wordSize, temp_reg);
 726     __ cmp_and_brx_short(temp_reg, argslot_reg, Assembler::lessUnsigned, Assembler::pt, loop);
 727   }
 728 
 729   // Now move the argslot down, to point to the opened-up space.
 730   __ add(argslot_reg, offset, argslot_reg);
 731   BLOCK_COMMENT("} insert_arg_slots");
 732 }
 733 
 734 
 735 // Helper to remove argument slots from the stack.
 736 // arg_slots must be a multiple of stack_move_unit() and > 0
 737 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
 738                                      RegisterOrConstant arg_slots,
 739                                      Register argslot_reg,
 740                                      Register temp_reg, Register temp2_reg, Register temp3_reg) {
 741   // allow constant zero
 742   if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
 743     return;
 744   assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
 745                              (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
 746 
 747   BLOCK_COMMENT("remove_arg_slots {");
 748   if (VerifyMethodHandles)
 749     verify_argslots(_masm, arg_slots, argslot_reg, temp_reg, temp2_reg, false,
 750                     "deleted argument(s) must fall within current frame");
 751   if (VerifyMethodHandles)
 752     verify_stack_move(_masm, arg_slots, +1);
 753 
 754   // Pull up everything shallower than argslot.
 755   // Then remove the excess space on the stack.
 756   // The stacked return address gets pulled up with everything else.
 757   // That is, copy [sp, argslot) upward by size words.  In pseudo-code:
 758   //   for (temp = argslot-1; temp >= sp; --temp)
 759   //     temp[size] = temp[0]
 760   //   argslot += size;
 761   //   sp += size;
 762 
 763   RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
 764   __ sub(argslot_reg, wordSize, temp_reg);  // source pointer for copy
 765 
 766   {
 767     Label L_loop;
 768     __ BIND(L_loop);
 769     // pull one word up each time through the loop
 770     __ ld_ptr(           Address(temp_reg, 0     ), temp2_reg);
 771     __ st_ptr(temp2_reg, Address(temp_reg, offset)           );
 772     __ sub(temp_reg, wordSize, temp_reg);
 773     __ cmp_and_brx_short(temp_reg, Gargs, Assembler::greaterEqualUnsigned, Assembler::pt, L_loop);
 774   }
 775 
 776   // And adjust the argslot address to point at the deletion point.
 777   __ add(argslot_reg, offset, argslot_reg);
 778 
 779   // We don't need the offset at this point anymore, just adjust SP and Gargs.
 780   (void) adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
 781 
 782   BLOCK_COMMENT("} remove_arg_slots");
 783 }
 784 
 785 // Helper to copy argument slots to the top of the stack.
 786 // The sequence starts with argslot_reg and is counted by slot_count
 787 // slot_count must be a multiple of stack_move_unit() and >= 0
 788 // This function blows the temps but does not change argslot_reg.
 789 void MethodHandles::push_arg_slots(MacroAssembler* _masm,
 790                                    Register argslot_reg,
 791                                    RegisterOrConstant slot_count,
 792                                    Register temp_reg, Register temp2_reg) {
 793   // allow constant zero
 794   if (slot_count.is_constant() && slot_count.as_constant() == 0)
 795     return;
 796   assert_different_registers(argslot_reg, temp_reg, temp2_reg,
 797                              (!slot_count.is_register() ? Gargs : slot_count.as_register()),
 798                              SP);
 799   assert(Interpreter::stackElementSize == wordSize, "else change this code");
 800 
 801   BLOCK_COMMENT("push_arg_slots {");
 802   if (VerifyMethodHandles)
 803     verify_stack_move(_masm, slot_count, 0);
 804 
 805   RegisterOrConstant offset = adjust_SP_and_Gargs_down_by_slots(_masm, slot_count, temp2_reg, temp_reg);
 806 
 807   if (slot_count.is_constant()) {
 808     for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
 809       __ ld_ptr(          Address(argslot_reg, i * wordSize), temp_reg);
 810       __ st_ptr(temp_reg, Address(Gargs,       i * wordSize));
 811     }
 812   } else {
 813     Label L_plural, L_loop, L_break;
 814     // Emit code to dynamically check for the common cases, zero and one slot.
 815     __ cmp(slot_count.as_register(), (int32_t) 1);
 816     __ br(Assembler::greater, false, Assembler::pn, L_plural);
 817     __ delayed()->nop();
 818     __ br(Assembler::less, false, Assembler::pn, L_break);
 819     __ delayed()->nop();
 820     __ ld_ptr(          Address(argslot_reg, 0), temp_reg);
 821     __ st_ptr(temp_reg, Address(Gargs,       0));
 822     __ ba_short(L_break);
 823     __ BIND(L_plural);
 824 
 825     // Loop for 2 or more:
 826     //   top = &argslot[slot_count]
 827     //   while (top > argslot)  *(--Gargs) = *(--top)
 828     Register top_reg = temp_reg;
 829     __ add(argslot_reg, offset, top_reg);
 830     __ add(Gargs,       offset, Gargs  );  // move back up again so we can go down
 831     __ BIND(L_loop);
 832     __ sub(top_reg, wordSize, top_reg);
 833     __ sub(Gargs,   wordSize, Gargs  );
 834     __ ld_ptr(           Address(top_reg, 0), temp2_reg);
 835     __ st_ptr(temp2_reg, Address(Gargs,   0));
 836     __ cmp_and_brx_short(top_reg, argslot_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
 837     __ BIND(L_break);
 838   }
 839   BLOCK_COMMENT("} push_arg_slots");
 840 }
 841 
 842 // in-place movement; no change to Gargs
 843 // blows temp_reg, temp2_reg
 844 void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
 845                                       Register bottom_reg,  // invariant
 846                                       Address  top_addr,    // can use temp_reg
 847                                       RegisterOrConstant positive_distance_in_slots,  // destroyed if register
 848                                       Register temp_reg, Register temp2_reg) {
 849   assert_different_registers(bottom_reg,
 850                              temp_reg, temp2_reg,
 851                              positive_distance_in_slots.register_or_noreg());
 852   BLOCK_COMMENT("move_arg_slots_up {");
 853   Label L_loop, L_break;
 854   Register top_reg = temp_reg;
 855   if (!top_addr.is_same_address(Address(top_reg, 0))) {
 856     __ add(top_addr, top_reg);
 857   }
 858   // Detect empty (or broken) loop:
 859 #ifdef ASSERT
 860   if (VerifyMethodHandles) {
 861     // Verify that &bottom < &top (non-empty interval)
 862     Label L_ok, L_bad;
 863     if (positive_distance_in_slots.is_register()) {
 864       __ cmp(positive_distance_in_slots.as_register(), (int32_t) 0);
 865       __ br(Assembler::lessEqual, false, Assembler::pn, L_bad);
 866       __ delayed()->nop();
 867     }
 868     __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
 869     __ BIND(L_bad);
 870     __ stop("valid bounds (copy up)");
 871     __ BIND(L_ok);
 872   }
 873 #endif
 874   __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
 875   // work top down to bottom, copying contiguous data upwards
 876   // In pseudo-code:
 877   //   while (--top >= bottom) *(top + distance) = *(top + 0);
 878   RegisterOrConstant offset = __ argument_offset(positive_distance_in_slots, positive_distance_in_slots.register_or_noreg());
 879   __ BIND(L_loop);
 880   __ sub(top_reg, wordSize, top_reg);
 881   __ ld_ptr(           Address(top_reg, 0     ), temp2_reg);
 882   __ st_ptr(temp2_reg, Address(top_reg, offset)           );
 883   __ cmp_and_brx_short(top_reg, bottom_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
 884   assert(Interpreter::stackElementSize == wordSize, "else change loop");
 885   __ BIND(L_break);
 886   BLOCK_COMMENT("} move_arg_slots_up");
 887 }
 888 
 889 // in-place movement; no change to rsp
 890 // blows temp_reg, temp2_reg
 891 void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
 892                                         Address  bottom_addr,  // can use temp_reg
 893                                         Register top_reg,      // invariant
 894                                         RegisterOrConstant negative_distance_in_slots,  // destroyed if register
 895                                         Register temp_reg, Register temp2_reg) {
 896   assert_different_registers(top_reg,
 897                              negative_distance_in_slots.register_or_noreg(),
 898                              temp_reg, temp2_reg);
 899   BLOCK_COMMENT("move_arg_slots_down {");
 900   Label L_loop, L_break;
 901   Register bottom_reg = temp_reg;
 902   if (!bottom_addr.is_same_address(Address(bottom_reg, 0))) {
 903     __ add(bottom_addr, bottom_reg);
 904   }
 905   // Detect empty (or broken) loop:
 906 #ifdef ASSERT
 907   assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
 908   if (VerifyMethodHandles) {
 909     // Verify that &bottom < &top (non-empty interval)
 910     Label L_ok, L_bad;
 911     if (negative_distance_in_slots.is_register()) {
 912       __ cmp(negative_distance_in_slots.as_register(), (int32_t) 0);
 913       __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
 914       __ delayed()->nop();
 915     }
 916     __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
 917     __ BIND(L_bad);
 918     __ stop("valid bounds (copy down)");
 919     __ BIND(L_ok);
 920   }
 921 #endif
 922   __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
 923   // work bottom up to top, copying contiguous data downwards
 924   // In pseudo-code:
 925   //   while (bottom < top) *(bottom - distance) = *(bottom + 0), bottom++;
 926   RegisterOrConstant offset = __ argument_offset(negative_distance_in_slots, negative_distance_in_slots.register_or_noreg());
 927   __ BIND(L_loop);
 928   __ ld_ptr(           Address(bottom_reg, 0     ), temp2_reg);
 929   __ st_ptr(temp2_reg, Address(bottom_reg, offset)           );
 930   __ add(bottom_reg, wordSize, bottom_reg);
 931   __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_loop);
 932   assert(Interpreter::stackElementSize == wordSize, "else change loop");
 933   __ BIND(L_break);
 934   BLOCK_COMMENT("} move_arg_slots_down");
 935 }
 936 
 937 // Copy from a field or array element to a stacked argument slot.
 938 // is_element (ignored) says whether caller is loading an array element instead of an instance field.
 939 void MethodHandles::move_typed_arg(MacroAssembler* _masm,
 940                                    BasicType type, bool is_element,
 941                                    Address value_src, Address slot_dest,
 942                                    Register temp_reg) {
 943   assert(!slot_dest.uses(temp_reg), "must be different register");
 944   BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
 945   if (type == T_OBJECT || type == T_ARRAY) {
 946     __ load_heap_oop(value_src, temp_reg);
 947     __ verify_oop(temp_reg);
 948     __ st_ptr(temp_reg, slot_dest);
 949   } else if (type != T_VOID) {
 950     int  arg_size      = type2aelembytes(type);
 951     bool arg_is_signed = is_signed_subword_type(type);
 952     int  slot_size     = is_subword_type(type) ? type2aelembytes(T_INT) : arg_size;  // store int sub-words as int
 953     __ load_sized_value( value_src, temp_reg, arg_size, arg_is_signed);
 954     __ store_sized_value(temp_reg, slot_dest, slot_size              );
 955   }
 956   BLOCK_COMMENT("} move_typed_arg");
 957 }
 958 
 959 // Cf. TemplateInterpreterGenerator::generate_return_entry_for and
 960 // InterpreterMacroAssembler::save_return_value
 961 void MethodHandles::move_return_value(MacroAssembler* _masm, BasicType type,
 962                                       Address return_slot) {
 963   BLOCK_COMMENT("move_return_value {");
 964   // Look at the type and pull the value out of the corresponding register.
 965   if (type == T_VOID) {
 966     // nothing to do
 967   } else if (type == T_OBJECT) {
 968     __ verify_oop(O0);
 969     __ st_ptr(O0, return_slot);
 970   } else if (type == T_INT || is_subword_type(type)) {
 971     int type_size = type2aelembytes(T_INT);
 972     __ store_sized_value(O0, return_slot, type_size);
 973   } else if (type == T_LONG) {
 974     // store the value by parts
 975     // Note: We assume longs are continguous (if misaligned) on the interpreter stack.
 976 #if !defined(_LP64) && defined(COMPILER2)
 977     __ stx(G1, return_slot);
 978 #else
 979   #ifdef _LP64
 980     __ stx(O0, return_slot);
 981   #else
 982     if (return_slot.has_disp()) {
 983       // The displacement is a constant
 984       __ st(O0, return_slot);
 985       __ st(O1, return_slot.plus_disp(Interpreter::stackElementSize));
 986     } else {
 987       __ std(O0, return_slot);
 988     }
 989   #endif
 990 #endif
 991   } else if (type == T_FLOAT) {
 992     __ stf(FloatRegisterImpl::S, Ftos_f, return_slot);
 993   } else if (type == T_DOUBLE) {
 994     __ stf(FloatRegisterImpl::D, Ftos_f, return_slot);
 995   } else {
 996     ShouldNotReachHere();
 997   }
 998   BLOCK_COMMENT("} move_return_value");
 999 }
1000 
1001 #ifndef PRODUCT
1002 extern "C" void print_method_handle(oop mh);
1003 void trace_method_handle_stub(const char* adaptername,
1004                               oopDesc* mh,
1005                               intptr_t* saved_sp) {
1006   bool has_mh = (strstr(adaptername, "return/") == NULL);  // return adapters don't have mh
1007   tty->print_cr("MH %s mh="INTPTR_FORMAT " saved_sp=" INTPTR_FORMAT, adaptername, (intptr_t) mh, saved_sp);
1008   if (has_mh)
1009     print_method_handle(mh);
1010 }
1011 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
1012   if (!TraceMethodHandles)  return;
1013   BLOCK_COMMENT("trace_method_handle {");
1014   // save: Gargs, O5_savedSP
1015   __ save_frame(16);
1016   __ set((intptr_t) adaptername, O0);
1017   __ mov(G3_method_handle, O1);
1018   __ mov(I5_savedSP, O2);
1019   __ mov(G3_method_handle, L3);
1020   __ mov(Gargs, L4);
1021   __ mov(G5_method_type, L5);
1022   __ call_VM_leaf(L7, CAST_FROM_FN_PTR(address, trace_method_handle_stub));
1023 
1024   __ mov(L3, G3_method_handle);
1025   __ mov(L4, Gargs);
1026   __ mov(L5, G5_method_type);
1027   __ restore();
1028   BLOCK_COMMENT("} trace_method_handle");
1029 }
1030 #endif // PRODUCT
1031 
1032 // which conversion op types are implemented here?
1033 int MethodHandles::adapter_conversion_ops_supported_mask() {
1034   return ((1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY)
1035          |(1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW)
1036          |(1<<java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST)
1037          |(1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM)
1038          |(1<<java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM)
1039           // OP_PRIM_TO_REF is below...
1040          |(1<<java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS)
1041          |(1<<java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS)
1042          |(1<<java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS)
1043          |(1<<java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS)
1044           // OP_COLLECT_ARGS is below...
1045          |(1<<java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS)
1046          |(!UseRicochetFrames ? 0 :
1047            java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() <= 0 ? 0 :
1048            ((1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF)
1049            |(1<<java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS)
1050            |(1<<java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS)
1051            )
1052           )
1053          );
1054 }
1055 
1056 //------------------------------------------------------------------------------
1057 // MethodHandles::generate_method_handle_stub
1058 //
1059 // Generate an "entry" field for a method handle.
1060 // This determines how the method handle will respond to calls.
1061 void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek) {
1062   MethodHandles::EntryKind ek_orig = ek_original_kind(ek);
1063 
1064   // Here is the register state during an interpreted call,
1065   // as set up by generate_method_handle_interpreter_entry():
1066   // - G5: garbage temp (was MethodHandle.invoke methodOop, unused)
1067   // - G3: receiver method handle
1068   // - O5_savedSP: sender SP (must preserve)
1069 
1070   const Register O0_scratch = O0;
1071   const Register O1_scratch = O1;
1072   const Register O2_scratch = O2;
1073   const Register O3_scratch = O3;
1074   const Register O4_scratch = O4;
1075   const Register G5_scratch = G5;
1076 
1077   // Often used names:
1078   const Register O0_argslot = O0;
1079 
1080   // Argument registers for _raise_exception:
1081   const Register O0_code     = O0;
1082   const Register O1_actual   = O1;
1083   const Register O2_required = O2;
1084 
1085   guarantee(java_lang_invoke_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
1086 
1087   // Some handy addresses:
1088   Address G3_mh_vmtarget(   G3_method_handle, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
1089 
1090   Address G3_dmh_vmindex(   G3_method_handle, java_lang_invoke_DirectMethodHandle::vmindex_offset_in_bytes());
1091 
1092   Address G3_bmh_vmargslot( G3_method_handle, java_lang_invoke_BoundMethodHandle::vmargslot_offset_in_bytes());
1093   Address G3_bmh_argument(  G3_method_handle, java_lang_invoke_BoundMethodHandle::argument_offset_in_bytes());
1094 
1095   Address G3_amh_vmargslot( G3_method_handle, java_lang_invoke_AdapterMethodHandle::vmargslot_offset_in_bytes());
1096   Address G3_amh_argument ( G3_method_handle, java_lang_invoke_AdapterMethodHandle::argument_offset_in_bytes());
1097   Address G3_amh_conversion(G3_method_handle, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
1098 
1099   const int java_mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
1100 
1101   if (have_entry(ek)) {
1102     __ nop();  // empty stubs make SG sick
1103     return;
1104   }
1105 
1106   address interp_entry = __ pc();
1107 
1108   trace_method_handle(_masm, entry_name(ek));
1109 
1110   BLOCK_COMMENT(err_msg("Entry %s {", entry_name(ek)));
1111 
1112   switch ((int) ek) {
1113   case _raise_exception:
1114     {
1115       // Not a real MH entry, but rather shared code for raising an
1116       // exception.  For sharing purposes the arguments are passed into registers
1117       // and then placed in the intepreter calling convention here.
1118       assert(raise_exception_method(), "must be set");
1119       assert(raise_exception_method()->from_compiled_entry(), "method must be linked");
1120 
1121       __ set(AddressLiteral((address) &_raise_exception_method), G5_method);
1122       __ ld_ptr(Address(G5_method, 0), G5_method);
1123 
1124       const int jobject_oop_offset = 0;
1125       __ ld_ptr(Address(G5_method, jobject_oop_offset), G5_method);
1126 
1127       adjust_SP_and_Gargs_down_by_slots(_masm, 3, noreg, noreg);
1128 
1129       __ st    (O0_code,     __ argument_address(constant(2), noreg, 0));
1130       __ st_ptr(O1_actual,   __ argument_address(constant(1), noreg, 0));
1131       __ st_ptr(O2_required, __ argument_address(constant(0), noreg, 0));
1132       jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1133     }
1134     break;
1135 
1136   case _invokestatic_mh:
1137   case _invokespecial_mh:
1138     {
1139       __ load_heap_oop(G3_mh_vmtarget, G5_method);  // target is a methodOop
1140       // Same as TemplateTable::invokestatic or invokespecial,
1141       // minus the CP setup and profiling:
1142       if (ek == _invokespecial_mh) {
1143         // Must load & check the first argument before entering the target method.
1144         __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1145         __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1146         __ null_check(G3_method_handle);
1147         __ verify_oop(G3_method_handle);
1148       }
1149       jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1150     }
1151     break;
1152 
1153   case _invokevirtual_mh:
1154     {
1155       // Same as TemplateTable::invokevirtual,
1156       // minus the CP setup and profiling:
1157 
1158       // Pick out the vtable index and receiver offset from the MH,
1159       // and then we can discard it:
1160       Register O2_index = O2_scratch;
1161       __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1162       __ ldsw(G3_dmh_vmindex, O2_index);
1163       // Note:  The verifier allows us to ignore G3_mh_vmtarget.
1164       __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1165       __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
1166 
1167       // Get receiver klass:
1168       Register O0_klass = O0_argslot;
1169       __ load_klass(G3_method_handle, O0_klass);
1170       __ verify_oop(O0_klass);
1171 
1172       // Get target methodOop & entry point:
1173       const int base = instanceKlass::vtable_start_offset() * wordSize;
1174       assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
1175 
1176       __ sll_ptr(O2_index, LogBytesPerWord, O2_index);
1177       __ add(O0_klass, O2_index, O0_klass);
1178       Address vtable_entry_addr(O0_klass, base + vtableEntry::method_offset_in_bytes());
1179       __ ld_ptr(vtable_entry_addr, G5_method);
1180 
1181       jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1182     }
1183     break;
1184 
1185   case _invokeinterface_mh:
1186     {
1187       // Same as TemplateTable::invokeinterface,
1188       // minus the CP setup and profiling:
1189       __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1190       Register O1_intf  = O1_scratch;
1191       Register G5_index = G5_scratch;
1192       __ load_heap_oop(G3_mh_vmtarget, O1_intf);
1193       __ ldsw(G3_dmh_vmindex, G5_index);
1194       __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1195       __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
1196 
1197       // Get receiver klass:
1198       Register O0_klass = O0_argslot;
1199       __ load_klass(G3_method_handle, O0_klass);
1200       __ verify_oop(O0_klass);
1201 
1202       // Get interface:
1203       Label no_such_interface;
1204       __ verify_oop(O1_intf);
1205       __ lookup_interface_method(O0_klass, O1_intf,
1206                                  // Note: next two args must be the same:
1207                                  G5_index, G5_method,
1208                                  O2_scratch,
1209                                  O3_scratch,
1210                                  no_such_interface);
1211 
1212       jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1213 
1214       __ bind(no_such_interface);
1215       // Throw an exception.
1216       // For historical reasons, it will be IncompatibleClassChangeError.
1217       __ unimplemented("not tested yet");
1218       __ ld_ptr(Address(O1_intf, java_mirror_offset), O2_required);  // required interface
1219       __ mov(   O0_klass,                             O1_actual);    // bad receiver
1220       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1221       __ delayed()->mov(Bytecodes::_invokeinterface,  O0_code);      // who is complaining?
1222     }
1223     break;
1224 
1225   case _bound_ref_mh:
1226   case _bound_int_mh:
1227   case _bound_long_mh:
1228   case _bound_ref_direct_mh:
1229   case _bound_int_direct_mh:
1230   case _bound_long_direct_mh:
1231     {
1232       const bool direct_to_method = (ek >= _bound_ref_direct_mh);
1233       BasicType arg_type  = ek_bound_mh_arg_type(ek);
1234       int       arg_slots = type2size[arg_type];
1235 
1236       // Make room for the new argument:
1237       load_vmargslot(_masm, G3_bmh_vmargslot, O0_argslot);
1238       __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1239 
1240       insert_arg_slots(_masm, arg_slots * stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1241 
1242       // Store bound argument into the new stack slot:
1243       __ load_heap_oop(G3_bmh_argument, O1_scratch);
1244       if (arg_type == T_OBJECT) {
1245         __ st_ptr(O1_scratch, Address(O0_argslot, 0));
1246       } else {
1247         Address prim_value_addr(O1_scratch, java_lang_boxing_object::value_offset_in_bytes(arg_type));
1248         move_typed_arg(_masm, arg_type, false,
1249                        prim_value_addr,
1250                        Address(O0_argslot, 0),
1251                        O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
1252       }
1253 
1254       if (direct_to_method) {
1255         __ load_heap_oop(G3_mh_vmtarget, G5_method);  // target is a methodOop
1256         jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1257       } else {
1258         __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);  // target is a methodOop
1259         __ verify_oop(G3_method_handle);
1260         __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1261       }
1262     }
1263     break;
1264 
1265   case _adapter_retype_only:
1266   case _adapter_retype_raw:
1267     // Immediately jump to the next MH layer:
1268     __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1269     __ verify_oop(G3_method_handle);
1270     __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1271     // This is OK when all parameter types widen.
1272     // It is also OK when a return type narrows.
1273     break;
1274 
1275   case _adapter_check_cast:
1276     {
1277       // Check a reference argument before jumping to the next layer of MH:
1278       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1279       Address vmarg = __ argument_address(O0_argslot, O0_argslot);
1280 
1281       // What class are we casting to?
1282       Register O1_klass = O1_scratch;  // Interesting AMH data.
1283       __ load_heap_oop(G3_amh_argument, O1_klass);  // This is a Class object!
1284       load_klass_from_Class(_masm, O1_klass, O2_scratch, O3_scratch);
1285 
1286       Label L_done;
1287       __ ld_ptr(vmarg, O2_scratch);
1288       __ br_null_short(O2_scratch, Assembler::pn, L_done);  // No cast if null.
1289       __ load_klass(O2_scratch, O2_scratch);
1290 
1291       // Live at this point:
1292       // - O0_argslot      :  argslot index in vmarg; may be required in the failing path
1293       // - O1_klass        :  klass required by the target method
1294       // - O2_scratch      :  argument klass to test
1295       // - G3_method_handle:  adapter method handle
1296       __ check_klass_subtype(O2_scratch, O1_klass, O3_scratch, O4_scratch, L_done);
1297 
1298       // If we get here, the type check failed!
1299       __ load_heap_oop(G3_amh_argument,        O2_required);  // required class
1300       __ ld_ptr(       vmarg,                  O1_actual);    // bad object
1301       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1302       __ delayed()->mov(Bytecodes::_checkcast, O0_code);      // who is complaining?
1303 
1304       __ BIND(L_done);
1305       // Get the new MH:
1306       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1307       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1308     }
1309     break;
1310 
1311   case _adapter_prim_to_prim:
1312   case _adapter_ref_to_prim:
1313     // Handled completely by optimized cases.
1314     __ stop("init_AdapterMethodHandle should not issue this");
1315     break;
1316 
1317   case _adapter_opt_i2i:        // optimized subcase of adapt_prim_to_prim
1318 //case _adapter_opt_f2i:        // optimized subcase of adapt_prim_to_prim
1319   case _adapter_opt_l2i:        // optimized subcase of adapt_prim_to_prim
1320   case _adapter_opt_unboxi:     // optimized subcase of adapt_ref_to_prim
1321     {
1322       // Perform an in-place conversion to int or an int subword.
1323       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1324       Address value;
1325       Address vmarg;
1326       bool value_left_justified = false;
1327 
1328       switch (ek) {
1329       case _adapter_opt_i2i:
1330         value = vmarg = __ argument_address(O0_argslot, O0_argslot);
1331         break;
1332       case _adapter_opt_l2i:
1333         {
1334           // just delete the extra slot
1335 #ifdef _LP64
1336           // In V9, longs are given 2 64-bit slots in the interpreter, but the
1337           // data is passed in only 1 slot.
1338           // Keep the second slot.
1339           __ add(__ argument_address(O0_argslot, O0_argslot, -1), O0_argslot);
1340           remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1341           value = Address(O0_argslot, 4);  // Get least-significant 32-bit of 64-bit value.
1342           vmarg = Address(O0_argslot, Interpreter::stackElementSize);
1343 #else
1344           // Keep the first slot.
1345           __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1346           remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1347           value = Address(O0_argslot, 0);
1348           vmarg = value;
1349 #endif
1350         }
1351         break;
1352       case _adapter_opt_unboxi:
1353         {
1354           vmarg = __ argument_address(O0_argslot, O0_argslot);
1355           // Load the value up from the heap.
1356           __ ld_ptr(vmarg, O1_scratch);
1357           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
1358 #ifdef ASSERT
1359           for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1360             if (is_subword_type(BasicType(bt)))
1361               assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
1362           }
1363 #endif
1364           __ null_check(O1_scratch, value_offset);
1365           value = Address(O1_scratch, value_offset);
1366 #ifdef _BIG_ENDIAN
1367           // Values stored in objects are packed.
1368           value_left_justified = true;
1369 #endif
1370         }
1371         break;
1372       default:
1373         ShouldNotReachHere();
1374       }
1375 
1376       // This check is required on _BIG_ENDIAN
1377       Register G5_vminfo = G5_scratch;
1378       __ ldsw(G3_amh_conversion, G5_vminfo);
1379       assert(CONV_VMINFO_SHIFT == 0, "preshifted");
1380 
1381       // Original 32-bit vmdata word must be of this form:
1382       // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
1383       __ lduw(value, O1_scratch);
1384       if (!value_left_justified)
1385         __ sll(O1_scratch, G5_vminfo, O1_scratch);
1386       Label zero_extend, done;
1387       __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
1388       __ br(Assembler::zero, false, Assembler::pn, zero_extend);
1389       __ delayed()->nop();
1390 
1391       // this path is taken for int->byte, int->short
1392       __ sra(O1_scratch, G5_vminfo, O1_scratch);
1393       __ ba_short(done);
1394 
1395       __ bind(zero_extend);
1396       // this is taken for int->char
1397       __ srl(O1_scratch, G5_vminfo, O1_scratch);
1398 
1399       __ bind(done);
1400       __ st(O1_scratch, vmarg);
1401 
1402       // Get the new MH:
1403       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1404       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1405     }
1406     break;
1407 
1408   case _adapter_opt_i2l:        // optimized subcase of adapt_prim_to_prim
1409   case _adapter_opt_unboxl:     // optimized subcase of adapt_ref_to_prim
1410     {
1411       // Perform an in-place int-to-long or ref-to-long conversion.
1412       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1413 
1414       // On big-endian machine we duplicate the slot and store the MSW
1415       // in the first slot.
1416       __ add(__ argument_address(O0_argslot, O0_argslot, 1), O0_argslot);
1417 
1418       insert_arg_slots(_masm, stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1419 
1420       Address arg_lsw(O0_argslot, 0);
1421       Address arg_msw(O0_argslot, -Interpreter::stackElementSize);
1422 
1423       switch (ek) {
1424       case _adapter_opt_i2l:
1425         {
1426 #ifdef _LP64
1427           __ ldsw(arg_lsw, O2_scratch);                 // Load LSW sign-extended
1428 #else
1429           __ ldsw(arg_lsw, O3_scratch);                 // Load LSW sign-extended
1430           __ srlx(O3_scratch, BitsPerInt, O2_scratch);  // Move MSW value to lower 32-bits for std
1431 #endif
1432           __ st_long(O2_scratch, arg_msw);              // Uses O2/O3 on !_LP64
1433         }
1434         break;
1435       case _adapter_opt_unboxl:
1436         {
1437           // Load the value up from the heap.
1438           __ ld_ptr(arg_lsw, O1_scratch);
1439           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
1440           assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
1441           __ null_check(O1_scratch, value_offset);
1442           __ ld_long(Address(O1_scratch, value_offset), O2_scratch);  // Uses O2/O3 on !_LP64
1443           __ st_long(O2_scratch, arg_msw);
1444         }
1445         break;
1446       default:
1447         ShouldNotReachHere();
1448       }
1449 
1450       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1451       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1452     }
1453     break;
1454 
1455   case _adapter_opt_f2d:        // optimized subcase of adapt_prim_to_prim
1456   case _adapter_opt_d2f:        // optimized subcase of adapt_prim_to_prim
1457     {
1458       // perform an in-place floating primitive conversion
1459       __ unimplemented(entry_name(ek));
1460     }
1461     break;
1462 
1463   case _adapter_prim_to_ref:
1464     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
1465     break;
1466 
1467   case _adapter_swap_args:
1468   case _adapter_rot_args:
1469     // handled completely by optimized cases
1470     __ stop("init_AdapterMethodHandle should not issue this");
1471     break;
1472 
1473   case _adapter_opt_swap_1:
1474   case _adapter_opt_swap_2:
1475   case _adapter_opt_rot_1_up:
1476   case _adapter_opt_rot_1_down:
1477   case _adapter_opt_rot_2_up:
1478   case _adapter_opt_rot_2_down:
1479     {
1480       int swap_slots = ek_adapter_opt_swap_slots(ek);
1481       int rotate     = ek_adapter_opt_swap_mode(ek);
1482 
1483       // 'argslot' is the position of the first argument to swap.
1484       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1485       __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1486       if (VerifyMethodHandles)
1487         verify_argslot(_masm, O0_argslot, O2_scratch, "swap point must fall within current frame");
1488 
1489       // 'vminfo' is the second.
1490       Register O1_destslot = O1_scratch;
1491       load_conversion_vminfo(_masm, G3_amh_conversion, O1_destslot);
1492       __ add(__ argument_address(O1_destslot, O1_destslot), O1_destslot);
1493       if (VerifyMethodHandles)
1494         verify_argslot(_masm, O1_destslot, O2_scratch, "swap point must fall within current frame");
1495 
1496       assert(Interpreter::stackElementSize == wordSize, "else rethink use of wordSize here");
1497       if (!rotate) {
1498         // simple swap
1499         for (int i = 0; i < swap_slots; i++) {
1500           __ ld_ptr(            Address(O0_argslot,  i * wordSize), O2_scratch);
1501           __ ld_ptr(            Address(O1_destslot, i * wordSize), O3_scratch);
1502           __ st_ptr(O3_scratch, Address(O0_argslot,  i * wordSize));
1503           __ st_ptr(O2_scratch, Address(O1_destslot, i * wordSize));
1504         }
1505       } else {
1506         // A rotate is actually pair of moves, with an "odd slot" (or pair)
1507         // changing place with a series of other slots.
1508         // First, push the "odd slot", which is going to get overwritten
1509         switch (swap_slots) {
1510         case 2 :  __ ld_ptr(Address(O0_argslot, 1 * wordSize), O4_scratch); // fall-thru
1511         case 1 :  __ ld_ptr(Address(O0_argslot, 0 * wordSize), O3_scratch); break;
1512         default:  ShouldNotReachHere();
1513         }
1514         if (rotate > 0) {
1515           // Here is rotate > 0:
1516           // (low mem)                                          (high mem)
1517           //     | dest:     more_slots...     | arg: odd_slot :arg+1 |
1518           // =>
1519           //     | dest: odd_slot | dest+1: more_slots...      :arg+1 |
1520           // work argslot down to destslot, copying contiguous data upwards
1521           // pseudo-code:
1522           //   argslot  = src_addr - swap_bytes
1523           //   destslot = dest_addr
1524           //   while (argslot >= destslot) *(argslot + swap_bytes) = *(argslot + 0), argslot--;
1525           move_arg_slots_up(_masm,
1526                             O1_destslot,
1527                             Address(O0_argslot, 0),
1528                             swap_slots,
1529                             O0_argslot, O2_scratch);
1530         } else {
1531           // Here is the other direction, rotate < 0:
1532           // (low mem)                                          (high mem)
1533           //     | arg: odd_slot | arg+1: more_slots...       :dest+1 |
1534           // =>
1535           //     | arg:    more_slots...     | dest: odd_slot :dest+1 |
1536           // work argslot up to destslot, copying contiguous data downwards
1537           // pseudo-code:
1538           //   argslot  = src_addr + swap_bytes
1539           //   destslot = dest_addr
1540           //   while (argslot <= destslot) *(argslot - swap_bytes) = *(argslot + 0), argslot++;
1541           // dest_slot denotes an exclusive upper limit
1542           int limit_bias = OP_ROT_ARGS_DOWN_LIMIT_BIAS;
1543           if (limit_bias != 0)
1544             __ add(O1_destslot, - limit_bias * wordSize, O1_destslot);
1545           move_arg_slots_down(_masm,
1546                               Address(O0_argslot, swap_slots * wordSize),
1547                               O1_destslot,
1548                               -swap_slots,
1549                               O0_argslot, O2_scratch);
1550 
1551           __ sub(O1_destslot, swap_slots * wordSize, O1_destslot);
1552         }
1553         // pop the original first chunk into the destination slot, now free
1554         switch (swap_slots) {
1555         case 2 :  __ st_ptr(O4_scratch, Address(O1_destslot, 1 * wordSize)); // fall-thru
1556         case 1 :  __ st_ptr(O3_scratch, Address(O1_destslot, 0 * wordSize)); break;
1557         default:  ShouldNotReachHere();
1558         }
1559       }
1560 
1561       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1562       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1563     }
1564     break;
1565 
1566   case _adapter_dup_args:
1567     {
1568       // 'argslot' is the position of the first argument to duplicate.
1569       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1570       __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1571 
1572       // 'stack_move' is negative number of words to duplicate.
1573       Register O1_stack_move = O1_scratch;
1574       load_stack_move(_masm, G3_amh_conversion, O1_stack_move);
1575 
1576       if (VerifyMethodHandles) {
1577         verify_argslots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, true,
1578                         "copied argument(s) must fall within current frame");
1579       }
1580 
1581       // insert location is always the bottom of the argument list:
1582       __ neg(O1_stack_move);
1583       push_arg_slots(_masm, O0_argslot, O1_stack_move, O2_scratch, O3_scratch);
1584 
1585       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1586       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1587     }
1588     break;
1589 
1590   case _adapter_drop_args:
1591     {
1592       // 'argslot' is the position of the first argument to nuke.
1593       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1594       __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1595 
1596       // 'stack_move' is number of words to drop.
1597       Register O1_stack_move = O1_scratch;
1598       load_stack_move(_masm, G3_amh_conversion, O1_stack_move);
1599 
1600       remove_arg_slots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, O4_scratch);
1601 
1602       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1603       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1604     }
1605     break;
1606 
1607   case _adapter_collect_args:
1608   case _adapter_fold_args:
1609   case _adapter_spread_args:
1610     // Handled completely by optimized cases.
1611     __ stop("init_AdapterMethodHandle should not issue this");
1612     break;
1613 
1614   case _adapter_opt_collect_ref:
1615   case _adapter_opt_collect_int:
1616   case _adapter_opt_collect_long:
1617   case _adapter_opt_collect_float:
1618   case _adapter_opt_collect_double:
1619   case _adapter_opt_collect_void:
1620   case _adapter_opt_collect_0_ref:
1621   case _adapter_opt_collect_1_ref:
1622   case _adapter_opt_collect_2_ref:
1623   case _adapter_opt_collect_3_ref:
1624   case _adapter_opt_collect_4_ref:
1625   case _adapter_opt_collect_5_ref:
1626   case _adapter_opt_filter_S0_ref:
1627   case _adapter_opt_filter_S1_ref:
1628   case _adapter_opt_filter_S2_ref:
1629   case _adapter_opt_filter_S3_ref:
1630   case _adapter_opt_filter_S4_ref:
1631   case _adapter_opt_filter_S5_ref:
1632   case _adapter_opt_collect_2_S0_ref:
1633   case _adapter_opt_collect_2_S1_ref:
1634   case _adapter_opt_collect_2_S2_ref:
1635   case _adapter_opt_collect_2_S3_ref:
1636   case _adapter_opt_collect_2_S4_ref:
1637   case _adapter_opt_collect_2_S5_ref:
1638   case _adapter_opt_fold_ref:
1639   case _adapter_opt_fold_int:
1640   case _adapter_opt_fold_long:
1641   case _adapter_opt_fold_float:
1642   case _adapter_opt_fold_double:
1643   case _adapter_opt_fold_void:
1644   case _adapter_opt_fold_1_ref:
1645   case _adapter_opt_fold_2_ref:
1646   case _adapter_opt_fold_3_ref:
1647   case _adapter_opt_fold_4_ref:
1648   case _adapter_opt_fold_5_ref:
1649     {
1650       // Given a fresh incoming stack frame, build a new ricochet frame.
1651       // On entry, TOS points at a return PC, and FP is the callers frame ptr.
1652       // RSI/R13 has the caller's exact stack pointer, which we must also preserve.
1653       // RCX contains an AdapterMethodHandle of the indicated kind.
1654 
1655       // Relevant AMH fields:
1656       // amh.vmargslot:
1657       //   points to the trailing edge of the arguments
1658       //   to filter, collect, or fold.  For a boxing operation,
1659       //   it points just after the single primitive value.
1660       // amh.argument:
1661       //   recursively called MH, on |collect| arguments
1662       // amh.vmtarget:
1663       //   final destination MH, on return value, etc.
1664       // amh.conversion.dest:
1665       //   tells what is the type of the return value
1666       //   (not needed here, since dest is also derived from ek)
1667       // amh.conversion.vminfo:
1668       //   points to the trailing edge of the return value
1669       //   when the vmtarget is to be called; this is
1670       //   equal to vmargslot + (retained ? |collect| : 0)
1671 
1672       // Pass 0 or more argument slots to the recursive target.
1673       int collect_count_constant = ek_adapter_opt_collect_count(ek);
1674 
1675       // The collected arguments are copied from the saved argument list:
1676       int collect_slot_constant = ek_adapter_opt_collect_slot(ek);
1677 
1678       assert(ek_orig == _adapter_collect_args ||
1679              ek_orig == _adapter_fold_args, "");
1680       bool retain_original_args = (ek_orig == _adapter_fold_args);
1681 
1682       // The return value is replaced (or inserted) at the 'vminfo' argslot.
1683       // Sometimes we can compute this statically.
1684       int dest_slot_constant = -1;
1685       if (!retain_original_args)
1686         dest_slot_constant = collect_slot_constant;
1687       else if (collect_slot_constant >= 0 && collect_count_constant >= 0)
1688         // We are preserving all the arguments, and the return value is prepended,
1689         // so the return slot is to the left (above) the |collect| sequence.
1690         dest_slot_constant = collect_slot_constant + collect_count_constant;
1691 
1692       // Replace all those slots by the result of the recursive call.
1693       // The result type can be one of ref, int, long, float, double, void.
1694       // In the case of void, nothing is pushed on the stack after return.
1695       BasicType dest = ek_adapter_opt_collect_type(ek);
1696       assert(dest == type2wfield[dest], "dest is a stack slot type");
1697       int dest_count = type2size[dest];
1698       assert(dest_count == 1 || dest_count == 2 || (dest_count == 0 && dest == T_VOID), "dest has a size");
1699 
1700       // Choose a return continuation.
1701       EntryKind ek_ret = _adapter_opt_return_any;
1702       if (dest != T_CONFLICT && OptimizeMethodHandles) {
1703         switch (dest) {
1704         case T_INT    : ek_ret = _adapter_opt_return_int;     break;
1705         case T_LONG   : ek_ret = _adapter_opt_return_long;    break;
1706         case T_FLOAT  : ek_ret = _adapter_opt_return_float;   break;
1707         case T_DOUBLE : ek_ret = _adapter_opt_return_double;  break;
1708         case T_OBJECT : ek_ret = _adapter_opt_return_ref;     break;
1709         case T_VOID   : ek_ret = _adapter_opt_return_void;    break;
1710         default       : ShouldNotReachHere();
1711         }
1712         if (dest == T_OBJECT && dest_slot_constant >= 0) {
1713           EntryKind ek_try = EntryKind(_adapter_opt_return_S0_ref + dest_slot_constant);
1714           if (ek_try <= _adapter_opt_return_LAST &&
1715               ek_adapter_opt_return_slot(ek_try) == dest_slot_constant) {
1716             ek_ret = ek_try;
1717           }
1718         }
1719         assert(ek_adapter_opt_return_type(ek_ret) == dest, "");
1720       }
1721 
1722       // Already pushed:  ... keep1 | collect | keep2 |
1723 
1724       // Push a few extra argument words, if we need them to store the return value.
1725       {
1726         int extra_slots = 0;
1727         if (retain_original_args) {
1728           extra_slots = dest_count;
1729         } else if (collect_count_constant == -1) {
1730           extra_slots = dest_count;  // collect_count might be zero; be generous
1731         } else if (dest_count > collect_count_constant) {
1732           extra_slots = (dest_count - collect_count_constant);
1733         } else {
1734           // else we know we have enough dead space in |collect| to repurpose for return values
1735         }
1736         if (extra_slots != 0) {
1737           __ sub(SP, round_to(extra_slots, 2) * Interpreter::stackElementSize, SP);
1738         }
1739       }
1740 
1741       // Set up Ricochet Frame.
1742       __ mov(SP, O5_savedSP);  // record SP for the callee
1743 
1744       // One extra (empty) slot for outgoing target MH (see Gargs computation below).
1745       __ save_frame(2);  // Note: we need to add 2 slots since frame::memory_parameter_word_sp_offset is 23.
1746 
1747       // Note: Gargs is live throughout the following, until we make our recursive call.
1748       // And the RF saves a copy in L4_saved_args_base.
1749 
1750       RicochetFrame::enter_ricochet_frame(_masm, G3_method_handle, Gargs,
1751                                           entry(ek_ret)->from_interpreted_entry());
1752 
1753       // Compute argument base:
1754       // Set up Gargs for current frame, extra (empty) slot is for outgoing target MH (space reserved by save_frame above).
1755       __ add(FP, STACK_BIAS - (1 * Interpreter::stackElementSize), Gargs);
1756 
1757       // Now pushed:  ... keep1 | collect | keep2 | extra | [RF]
1758 
1759 #ifdef ASSERT
1760       if (VerifyMethodHandles && dest != T_CONFLICT) {
1761         BLOCK_COMMENT("verify AMH.conv.dest {");
1762         extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O1_scratch);
1763         Label L_dest_ok;
1764         __ cmp(O1_scratch, (int) dest);
1765         __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
1766         __ delayed()->nop();
1767         if (dest == T_INT) {
1768           for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1769             if (is_subword_type(BasicType(bt))) {
1770               __ cmp(O1_scratch, (int) bt);
1771               __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
1772               __ delayed()->nop();
1773             }
1774           }
1775         }
1776         __ stop("bad dest in AMH.conv");
1777         __ BIND(L_dest_ok);
1778         BLOCK_COMMENT("} verify AMH.conv.dest");
1779       }
1780 #endif //ASSERT
1781 
1782       // Find out where the original copy of the recursive argument sequence begins.
1783       Register O0_coll = O0_scratch;
1784       {
1785         RegisterOrConstant collect_slot = collect_slot_constant;
1786         if (collect_slot_constant == -1) {
1787           load_vmargslot(_masm, G3_amh_vmargslot, O1_scratch);
1788           collect_slot = O1_scratch;
1789         }
1790         // collect_slot might be 0, but we need the move anyway.
1791         __ add(RicochetFrame::L4_saved_args_base, __ argument_offset(collect_slot, collect_slot.register_or_noreg()), O0_coll);
1792         // O0_coll now points at the trailing edge of |collect| and leading edge of |keep2|
1793       }
1794 
1795       // Replace the old AMH with the recursive MH.  (No going back now.)
1796       // In the case of a boxing call, the recursive call is to a 'boxer' method,
1797       // such as Integer.valueOf or Long.valueOf.  In the case of a filter
1798       // or collect call, it will take one or more arguments, transform them,
1799       // and return some result, to store back into argument_base[vminfo].
1800       __ load_heap_oop(G3_amh_argument, G3_method_handle);
1801       if (VerifyMethodHandles)  verify_method_handle(_masm, G3_method_handle, O1_scratch, O2_scratch);
1802 
1803       // Calculate |collect|, the number of arguments we are collecting.
1804       Register O1_collect_count = O1_scratch;
1805       RegisterOrConstant collect_count;
1806       if (collect_count_constant < 0) {
1807         __ load_method_handle_vmslots(O1_collect_count, G3_method_handle, O2_scratch);
1808         collect_count = O1_collect_count;
1809       } else {
1810         collect_count = collect_count_constant;
1811 #ifdef ASSERT
1812         if (VerifyMethodHandles) {
1813           BLOCK_COMMENT("verify collect_count_constant {");
1814           __ load_method_handle_vmslots(O3_scratch, G3_method_handle, O2_scratch);
1815           Label L_count_ok;
1816           __ cmp_and_br_short(O3_scratch, collect_count_constant, Assembler::equal, Assembler::pt, L_count_ok);
1817           __ stop("bad vminfo in AMH.conv");
1818           __ BIND(L_count_ok);
1819           BLOCK_COMMENT("} verify collect_count_constant");
1820         }
1821 #endif //ASSERT
1822       }
1823 
1824       // copy |collect| slots directly to TOS:
1825       push_arg_slots(_masm, O0_coll, collect_count, O2_scratch, O3_scratch);
1826       // Now pushed:  ... keep1 | collect | keep2 | RF... | collect |
1827       // O0_coll still points at the trailing edge of |collect| and leading edge of |keep2|
1828 
1829       // If necessary, adjust the saved arguments to make room for the eventual return value.
1830       // Normal adjustment:  ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
1831       // If retaining args:  ... keep1 | +dest+ |  collect  | keep2 | RF... | collect |
1832       // In the non-retaining case, this might move keep2 either up or down.
1833       // We don't have to copy the whole | RF... collect | complex,
1834       // but we must adjust RF.saved_args_base.
1835       // Also, from now on, we will forget about the original copy of |collect|.
1836       // If we are retaining it, we will treat it as part of |keep2|.
1837       // For clarity we will define |keep3| = |collect|keep2| or |keep2|.
1838 
1839       BLOCK_COMMENT("adjust trailing arguments {");
1840       // Compare the sizes of |+dest+| and |-collect-|, which are opposed opening and closing movements.
1841       int                open_count  = dest_count;
1842       RegisterOrConstant close_count = collect_count_constant;
1843       Register O1_close_count = O1_collect_count;
1844       if (retain_original_args) {
1845         close_count = constant(0);
1846       } else if (collect_count_constant == -1) {
1847         close_count = O1_collect_count;
1848       }
1849 
1850       // How many slots need moving?  This is simply dest_slot (0 => no |keep3|).
1851       RegisterOrConstant keep3_count;
1852       Register O2_keep3_count = O2_scratch;
1853       if (dest_slot_constant < 0) {
1854         extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O2_keep3_count);
1855         keep3_count = O2_keep3_count;
1856       } else  {
1857         keep3_count = dest_slot_constant;
1858 #ifdef ASSERT
1859         if (VerifyMethodHandles && dest_slot_constant < 0) {
1860           BLOCK_COMMENT("verify dest_slot_constant {");
1861           extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O3_scratch);
1862           Label L_vminfo_ok;
1863           __ cmp_and_br_short(O3_scratch, dest_slot_constant, Assembler::equal, Assembler::pt, L_vminfo_ok);
1864           __ stop("bad vminfo in AMH.conv");
1865           __ BIND(L_vminfo_ok);
1866           BLOCK_COMMENT("} verify dest_slot_constant");
1867         }
1868 #endif //ASSERT
1869       }
1870 
1871       // tasks remaining:
1872       bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
1873       bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
1874       bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());
1875 
1876       // Old and new argument locations (based at slot 0).
1877       // Net shift (&new_argv - &old_argv) is (close_count - open_count).
1878       bool zero_open_count = (open_count == 0);  // remember this bit of info
1879       if (move_keep3 && fix_arg_base) {
1880         // It will be easier to have everything in one register:
1881         if (close_count.is_register()) {
1882           // Deduct open_count from close_count register to get a clean +/- value.
1883           __ sub(close_count.as_register(), open_count, close_count.as_register());
1884         } else {
1885           close_count = close_count.as_constant() - open_count;
1886         }
1887         open_count = 0;
1888       }
1889       Register L4_old_argv = RicochetFrame::L4_saved_args_base;
1890       Register O3_new_argv = O3_scratch;
1891       if (fix_arg_base) {
1892         __ add(L4_old_argv, __ argument_offset(close_count, O4_scratch), O3_new_argv,
1893                -(open_count * Interpreter::stackElementSize));
1894       }
1895 
1896       // First decide if any actual data are to be moved.
1897       // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
1898       // (As it happens, all movements involve an argument list size change.)
1899 
1900       // If there are variable parameters, use dynamic checks to skip around the whole mess.
1901       Label L_done;
1902       if (keep3_count.is_register()) {
1903         __ cmp_and_br_short(keep3_count.as_register(), 0, Assembler::equal, Assembler::pn, L_done);
1904       }
1905       if (close_count.is_register()) {
1906         __ cmp_and_br_short(close_count.as_register(), open_count, Assembler::equal, Assembler::pn, L_done);
1907       }
1908 
1909       if (move_keep3 && fix_arg_base) {
1910         bool emit_move_down = false, emit_move_up = false, emit_guard = false;
1911         if (!close_count.is_constant()) {
1912           emit_move_down = emit_guard = !zero_open_count;
1913           emit_move_up   = true;
1914         } else if (open_count != close_count.as_constant()) {
1915           emit_move_down = (open_count > close_count.as_constant());
1916           emit_move_up   = !emit_move_down;
1917         }
1918         Label L_move_up;
1919         if (emit_guard) {
1920           __ cmp(close_count.as_register(), open_count);
1921           __ br(Assembler::greater, false, Assembler::pn, L_move_up);
1922           __ delayed()->nop();
1923         }
1924 
1925         if (emit_move_down) {
1926           // Move arguments down if |+dest+| > |-collect-|
1927           // (This is rare, except when arguments are retained.)
1928           // This opens space for the return value.
1929           if (keep3_count.is_constant()) {
1930             for (int i = 0; i < keep3_count.as_constant(); i++) {
1931               __ ld_ptr(            Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1932               __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize)            );
1933             }
1934           } else {
1935             // Live: O1_close_count, O2_keep3_count, O3_new_argv
1936             Register argv_top = O0_scratch;
1937             __ add(L4_old_argv, __ argument_offset(keep3_count, O4_scratch), argv_top);
1938             move_arg_slots_down(_masm,
1939                                 Address(L4_old_argv, 0),  // beginning of old argv
1940                                 argv_top,                 // end of old argv
1941                                 close_count,              // distance to move down (must be negative)
1942                                 O4_scratch, G5_scratch);
1943           }
1944         }
1945 
1946         if (emit_guard) {
1947           __ ba_short(L_done);  // assumes emit_move_up is true also
1948           __ BIND(L_move_up);
1949         }
1950 
1951         if (emit_move_up) {
1952           // Move arguments up if |+dest+| < |-collect-|
1953           // (This is usual, except when |keep3| is empty.)
1954           // This closes up the space occupied by the now-deleted collect values.
1955           if (keep3_count.is_constant()) {
1956             for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
1957               __ ld_ptr(            Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1958               __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize)            );
1959             }
1960           } else {
1961             Address argv_top(L4_old_argv, __ argument_offset(keep3_count, O4_scratch));
1962             // Live: O1_close_count, O2_keep3_count, O3_new_argv
1963             move_arg_slots_up(_masm,
1964                               L4_old_argv,  // beginning of old argv
1965                               argv_top,     // end of old argv
1966                               close_count,  // distance to move up (must be positive)
1967                               O4_scratch, G5_scratch);
1968           }
1969         }
1970       }
1971       __ BIND(L_done);
1972 
1973       if (fix_arg_base) {
1974         // adjust RF.saved_args_base
1975         __ mov(O3_new_argv, RicochetFrame::L4_saved_args_base);
1976       }
1977 
1978       if (stomp_dest) {
1979         // Stomp the return slot, so it doesn't hold garbage.
1980         // This isn't strictly necessary, but it may help detect bugs.
1981         __ set(RicochetFrame::RETURN_VALUE_PLACEHOLDER, O4_scratch);
1982         __ st_ptr(O4_scratch, Address(RicochetFrame::L4_saved_args_base,
1983                                       __ argument_offset(keep3_count, keep3_count.register_or_noreg())));  // uses O2_keep3_count
1984       }
1985       BLOCK_COMMENT("} adjust trailing arguments");
1986 
1987       BLOCK_COMMENT("do_recursive_call");
1988       __ mov(SP, O5_savedSP);  // record SP for the callee
1989       __ set(ExternalAddress(SharedRuntime::ricochet_blob()->bounce_addr() - frame::pc_return_offset), O7);
1990       // The globally unique bounce address has two purposes:
1991       // 1. It helps the JVM recognize this frame (frame::is_ricochet_frame).
1992       // 2. When returned to, it cuts back the stack and redirects control flow
1993       //    to the return handler.
1994       // The return handler will further cut back the stack when it takes
1995       // down the RF.  Perhaps there is a way to streamline this further.
1996 
1997       // State during recursive call:
1998       // ... keep1 | dest | dest=42 | keep3 | RF... | collect | bounce_pc |
1999       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2000     }
2001     break;
2002 
2003   case _adapter_opt_return_ref:
2004   case _adapter_opt_return_int:
2005   case _adapter_opt_return_long:
2006   case _adapter_opt_return_float:
2007   case _adapter_opt_return_double:
2008   case _adapter_opt_return_void:
2009   case _adapter_opt_return_S0_ref:
2010   case _adapter_opt_return_S1_ref:
2011   case _adapter_opt_return_S2_ref:
2012   case _adapter_opt_return_S3_ref:
2013   case _adapter_opt_return_S4_ref:
2014   case _adapter_opt_return_S5_ref:
2015     {
2016       BasicType dest_type_constant = ek_adapter_opt_return_type(ek);
2017       int       dest_slot_constant = ek_adapter_opt_return_slot(ek);
2018 
2019       if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);
2020 
2021       if (dest_slot_constant == -1) {
2022         // The current stub is a general handler for this dest_type.
2023         // It can be called from _adapter_opt_return_any below.
2024         // Stash the address in a little table.
2025         assert((dest_type_constant & CONV_TYPE_MASK) == dest_type_constant, "oob");
2026         address return_handler = __ pc();
2027         _adapter_return_handlers[dest_type_constant] = return_handler;
2028         if (dest_type_constant == T_INT) {
2029           // do the subword types too
2030           for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
2031             if (is_subword_type(BasicType(bt)) &&
2032                 _adapter_return_handlers[bt] == NULL) {
2033               _adapter_return_handlers[bt] = return_handler;
2034             }
2035           }
2036         }
2037       }
2038 
2039       // On entry to this continuation handler, make Gargs live again.
2040       __ mov(RicochetFrame::L4_saved_args_base, Gargs);
2041 
2042       Register O7_temp   = O7;
2043       Register O5_vminfo = O5;
2044 
2045       RegisterOrConstant dest_slot = dest_slot_constant;
2046       if (dest_slot_constant == -1) {
2047         extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O5_vminfo);
2048         dest_slot = O5_vminfo;
2049       }
2050       // Store the result back into the argslot.
2051       // This code uses the interpreter calling sequence, in which the return value
2052       // is usually left in the TOS register, as defined by InterpreterMacroAssembler::pop.
2053       // There are certain irregularities with floating point values, which can be seen
2054       // in TemplateInterpreterGenerator::generate_return_entry_for.
2055       move_return_value(_masm, dest_type_constant, __ argument_address(dest_slot, O7_temp));
2056 
2057       RicochetFrame::leave_ricochet_frame(_masm, G3_method_handle, I5_savedSP, I7);
2058 
2059       // Load the final target and go.
2060       if (VerifyMethodHandles)  verify_method_handle(_masm, G3_method_handle, O0_scratch, O1_scratch);
2061       __ restore(I5_savedSP, G0, SP);
2062       __ jump_to_method_handle_entry(G3_method_handle, O0_scratch);
2063       __ illtrap(0);
2064     }
2065     break;
2066 
2067   case _adapter_opt_return_any:
2068     {
2069       Register O7_temp      = O7;
2070       Register O5_dest_type = O5;
2071 
2072       if (VerifyMethodHandles)  RicochetFrame::verify_clean(_masm);
2073       extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O5_dest_type);
2074       __ set(ExternalAddress((address) &_adapter_return_handlers[0]), O7_temp);
2075       __ sll_ptr(O5_dest_type, LogBytesPerWord, O5_dest_type);
2076       __ ld_ptr(O7_temp, O5_dest_type, O7_temp);
2077 
2078 #ifdef ASSERT
2079       { Label L_ok;
2080         __ br_notnull_short(O7_temp, Assembler::pt, L_ok);
2081         __ stop("bad method handle return");
2082         __ BIND(L_ok);
2083       }
2084 #endif //ASSERT
2085       __ JMP(O7_temp, 0);
2086       __ delayed()->nop();
2087     }
2088     break;
2089 
2090   case _adapter_opt_spread_0:
2091   case _adapter_opt_spread_1_ref:
2092   case _adapter_opt_spread_2_ref:
2093   case _adapter_opt_spread_3_ref:
2094   case _adapter_opt_spread_4_ref:
2095   case _adapter_opt_spread_5_ref:
2096   case _adapter_opt_spread_ref:
2097   case _adapter_opt_spread_byte:
2098   case _adapter_opt_spread_char:
2099   case _adapter_opt_spread_short:
2100   case _adapter_opt_spread_int:
2101   case _adapter_opt_spread_long:
2102   case _adapter_opt_spread_float:
2103   case _adapter_opt_spread_double:
2104     {
2105       // spread an array out into a group of arguments
2106       int  length_constant    = ek_adapter_opt_spread_count(ek);
2107       bool length_can_be_zero = (length_constant == 0);
2108       if (length_constant < 0) {
2109         // some adapters with variable length must handle the zero case
2110         if (!OptimizeMethodHandles ||
2111             ek_adapter_opt_spread_type(ek) != T_OBJECT)
2112           length_can_be_zero = true;
2113       }
2114 
2115       // find the address of the array argument
2116       load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
2117       __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
2118 
2119       // O0_argslot points both to the array and to the first output arg
2120       Address vmarg = Address(O0_argslot, 0);
2121 
2122       // Get the array value.
2123       Register  O1_array       = O1_scratch;
2124       Register  O2_array_klass = O2_scratch;
2125       BasicType elem_type      = ek_adapter_opt_spread_type(ek);
2126       int       elem_slots     = type2size[elem_type];  // 1 or 2
2127       int       array_slots    = 1;  // array is always a T_OBJECT
2128       int       length_offset  = arrayOopDesc::length_offset_in_bytes();
2129       int       elem0_offset   = arrayOopDesc::base_offset_in_bytes(elem_type);
2130       __ ld_ptr(vmarg, O1_array);
2131 
2132       Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
2133       if (length_can_be_zero) {
2134         // handle the null pointer case, if zero is allowed
2135         Label L_skip;
2136         if (length_constant < 0) {
2137           load_conversion_vminfo(_masm, G3_amh_conversion, O3_scratch);
2138           __ cmp_zero_and_br(Assembler::notZero, O3_scratch, L_skip);
2139           __ delayed()->nop(); // to avoid back-to-back cbcond instructions
2140         }
2141         __ br_null_short(O1_array, Assembler::pn, L_array_is_empty);
2142         __ BIND(L_skip);
2143       }
2144       __ null_check(O1_array, oopDesc::klass_offset_in_bytes());
2145       __ load_klass(O1_array, O2_array_klass);
2146 
2147       // Check the array type.
2148       Register O3_klass = O3_scratch;
2149       __ load_heap_oop(G3_amh_argument, O3_klass);  // this is a Class object!
2150       load_klass_from_Class(_masm, O3_klass, O4_scratch, G5_scratch);
2151 
2152       Label L_ok_array_klass, L_bad_array_klass, L_bad_array_length;
2153       __ check_klass_subtype(O2_array_klass, O3_klass, O4_scratch, G5_scratch, L_ok_array_klass);
2154       // If we get here, the type check failed!
2155       __ ba_short(L_bad_array_klass);
2156       __ BIND(L_ok_array_klass);
2157 
2158       // Check length.
2159       if (length_constant >= 0) {
2160         __ ldsw(Address(O1_array, length_offset), O4_scratch);
2161         __ cmp(O4_scratch, length_constant);
2162       } else {
2163         Register O3_vminfo = O3_scratch;
2164         load_conversion_vminfo(_masm, G3_amh_conversion, O3_vminfo);
2165         __ ldsw(Address(O1_array, length_offset), O4_scratch);
2166         __ cmp(O3_vminfo, O4_scratch);
2167       }
2168       __ br(Assembler::notEqual, false, Assembler::pn, L_bad_array_length);
2169       __ delayed()->nop();
2170 
2171       Register O2_argslot_limit = O2_scratch;
2172 
2173       // Array length checks out.  Now insert any required stack slots.
2174       if (length_constant == -1) {
2175         // Form a pointer to the end of the affected region.
2176         __ add(O0_argslot, Interpreter::stackElementSize, O2_argslot_limit);
2177         // 'stack_move' is negative number of words to insert
2178         // This number already accounts for elem_slots.
2179         Register O3_stack_move = O3_scratch;
2180         load_stack_move(_masm, G3_amh_conversion, O3_stack_move);
2181         __ cmp(O3_stack_move, 0);
2182         assert(stack_move_unit() < 0, "else change this comparison");
2183         __ br(Assembler::less, false, Assembler::pn, L_insert_arg_space);
2184         __ delayed()->nop();
2185         __ br(Assembler::equal, false, Assembler::pn, L_copy_args);
2186         __ delayed()->nop();
2187         // single argument case, with no array movement
2188         __ BIND(L_array_is_empty);
2189         remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2190                          O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2191         __ ba_short(L_args_done);  // no spreading to do
2192         __ BIND(L_insert_arg_space);
2193         // come here in the usual case, stack_move < 0 (2 or more spread arguments)
2194         // Live: O1_array, O2_argslot_limit, O3_stack_move
2195         insert_arg_slots(_masm, O3_stack_move,
2196                          O0_argslot, O4_scratch, G5_scratch, O1_scratch);
2197         // reload from rdx_argslot_limit since rax_argslot is now decremented
2198         __ ld_ptr(Address(O2_argslot_limit, -Interpreter::stackElementSize), O1_array);
2199       } else if (length_constant >= 1) {
2200         int new_slots = (length_constant * elem_slots) - array_slots;
2201         insert_arg_slots(_masm, new_slots * stack_move_unit(),
2202                          O0_argslot, O2_scratch, O3_scratch, O4_scratch);
2203       } else if (length_constant == 0) {
2204         __ BIND(L_array_is_empty);
2205         remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2206                          O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2207       } else {
2208         ShouldNotReachHere();
2209       }
2210 
2211       // Copy from the array to the new slots.
2212       // Note: Stack change code preserves integrity of O0_argslot pointer.
2213       // So even after slot insertions, O0_argslot still points to first argument.
2214       // Beware:  Arguments that are shallow on the stack are deep in the array,
2215       // and vice versa.  So a downward-growing stack (the usual) has to be copied
2216       // elementwise in reverse order from the source array.
2217       __ BIND(L_copy_args);
2218       if (length_constant == -1) {
2219         // [O0_argslot, O2_argslot_limit) is the area we are inserting into.
2220         // Array element [0] goes at O0_argslot_limit[-wordSize].
2221         Register O1_source = O1_array;
2222         __ add(Address(O1_array, elem0_offset), O1_source);
2223         Register O4_fill_ptr = O4_scratch;
2224         __ mov(O2_argslot_limit, O4_fill_ptr);
2225         Label L_loop;
2226         __ BIND(L_loop);
2227         __ add(O4_fill_ptr, -Interpreter::stackElementSize * elem_slots, O4_fill_ptr);
2228         move_typed_arg(_masm, elem_type, true,
2229                        Address(O1_source, 0), Address(O4_fill_ptr, 0),
2230                        O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
2231         __ add(O1_source, type2aelembytes(elem_type), O1_source);
2232         __ cmp_and_brx_short(O4_fill_ptr, O0_argslot, Assembler::greaterUnsigned, Assembler::pt, L_loop);
2233       } else if (length_constant == 0) {
2234         // nothing to copy
2235       } else {
2236         int elem_offset = elem0_offset;
2237         int slot_offset = length_constant * Interpreter::stackElementSize;
2238         for (int index = 0; index < length_constant; index++) {
2239           slot_offset -= Interpreter::stackElementSize * elem_slots;  // fill backward
2240           move_typed_arg(_masm, elem_type, true,
2241                          Address(O1_array, elem_offset), Address(O0_argslot, slot_offset),
2242                          O2_scratch);  // must be an even register for !_LP64 long moves (uses O2/O3)
2243           elem_offset += type2aelembytes(elem_type);
2244         }
2245       }
2246       __ BIND(L_args_done);
2247 
2248       // Arguments are spread.  Move to next method handle.
2249       __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
2250       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2251 
2252       __ BIND(L_bad_array_klass);
2253       assert(!vmarg.uses(O2_required), "must be different registers");
2254       __ load_heap_oop(Address(O2_array_klass, java_mirror_offset), O2_required);  // required class
2255       __ ld_ptr(       vmarg,                                       O1_actual);    // bad object
2256       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
2257       __ delayed()->mov(Bytecodes::_aaload,                         O0_code);      // who is complaining?
2258 
2259       __ bind(L_bad_array_length);
2260       assert(!vmarg.uses(O2_required), "must be different registers");
2261       __ mov(   G3_method_handle,                O2_required);  // required class
2262       __ ld_ptr(vmarg,                           O1_actual);    // bad object
2263       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
2264       __ delayed()->mov(Bytecodes::_arraylength, O0_code);      // who is complaining?
2265     }
2266     break;
2267 
2268   default:
2269     DEBUG_ONLY(tty->print_cr("bad ek=%d (%s)", (int)ek, entry_name(ek)));
2270     ShouldNotReachHere();
2271   }
2272   BLOCK_COMMENT(err_msg("} Entry %s", entry_name(ek)));
2273 
2274   address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
2275   __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
2276 
2277   init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
2278 }