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