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