1 /*
   2  * Copyright (c) 1997, 2010, 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 #ifdef ASSERT
  73 static void verify_argslot(MacroAssembler* _masm, Register argslot_reg,
  74                            const char* error_message) {
  75   // Verify that argslot lies within (rsp, rbp].
  76   Label L_ok, L_bad;
  77   BLOCK_COMMENT("{ verify_argslot");
  78   __ cmpptr(argslot_reg, rbp);
  79   __ jccb(Assembler::above, L_bad);
  80   __ cmpptr(rsp, argslot_reg);
  81   __ jccb(Assembler::below, L_ok);
  82   __ bind(L_bad);
  83   __ stop(error_message);
  84   __ bind(L_ok);
  85   BLOCK_COMMENT("} verify_argslot");
  86 }
  87 #endif
  88 
  89 
  90 // Code generation
  91 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
  92   // rbx: methodOop
  93   // rcx: receiver method handle (must load from sp[MethodTypeForm.vmslots])
  94   // rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
  95   // rdx, rdi: garbage temp, blown away
  96 
  97   Register rbx_method = rbx;
  98   Register rcx_recv   = rcx;
  99   Register rax_mtype  = rax;
 100   Register rdx_temp   = rdx;
 101   Register rdi_temp   = rdi;
 102 
 103   // emit WrongMethodType path first, to enable jccb back-branch from main path
 104   Label wrong_method_type;
 105   __ bind(wrong_method_type);
 106   Label invoke_generic_slow_path;
 107   assert(methodOopDesc::intrinsic_id_size_in_bytes() == sizeof(u1), "");;
 108   __ cmpb(Address(rbx_method, methodOopDesc::intrinsic_id_offset_in_bytes()), (int) vmIntrinsics::_invokeExact);
 109   __ jcc(Assembler::notEqual, invoke_generic_slow_path);
 110   __ push(rax_mtype);       // required mtype
 111   __ push(rcx_recv);        // bad mh (1st stacked argument)
 112   __ jump(ExternalAddress(Interpreter::throw_WrongMethodType_entry()));
 113 
 114   // here's where control starts out:
 115   __ align(CodeEntryAlignment);
 116   address entry_point = __ pc();
 117 
 118   // fetch the MethodType from the method handle into rax (the 'check' register)
 119   {
 120     Register tem = rbx_method;
 121     for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
 122       __ movptr(rax_mtype, Address(tem, *pchase));
 123       tem = rax_mtype;          // in case there is another indirection
 124     }
 125   }
 126 
 127   // given the MethodType, find out where the MH argument is buried
 128   __ load_heap_oop(rdx_temp, Address(rax_mtype, __ delayed_value(java_dyn_MethodType::form_offset_in_bytes, rdi_temp)));
 129   Register rdx_vmslots = rdx_temp;
 130   __ movl(rdx_vmslots, Address(rdx_temp, __ delayed_value(java_dyn_MethodTypeForm::vmslots_offset_in_bytes, rdi_temp)));
 131   __ movptr(rcx_recv, __ argument_address(rdx_vmslots));
 132 
 133   trace_method_handle(_masm, "invokeExact");
 134 
 135   __ check_method_handle_type(rax_mtype, rcx_recv, rdi_temp, wrong_method_type);
 136   __ jump_to_method_handle_entry(rcx_recv, rdi_temp);
 137 
 138   // for invokeGeneric (only), apply argument and result conversions on the fly
 139   __ bind(invoke_generic_slow_path);
 140 #ifdef ASSERT
 141   { Label L;
 142     __ cmpb(Address(rbx_method, methodOopDesc::intrinsic_id_offset_in_bytes()), (int) vmIntrinsics::_invokeGeneric);
 143     __ jcc(Assembler::equal, L);
 144     __ stop("bad methodOop::intrinsic_id");
 145     __ bind(L);
 146   }
 147 #endif //ASSERT
 148   Register rbx_temp = rbx_method;  // don't need it now
 149 
 150   // make room on the stack for another pointer:
 151   Register rcx_argslot = rcx_recv;
 152   __ lea(rcx_argslot, __ argument_address(rdx_vmslots, 1));
 153   insert_arg_slots(_masm, 2 * stack_move_unit(), _INSERT_REF_MASK,
 154                    rcx_argslot, rbx_temp, rdx_temp);
 155 
 156   // load up an adapter from the calling type (Java weaves this)
 157   __ load_heap_oop(rdx_temp, Address(rax_mtype, __ delayed_value(java_dyn_MethodType::form_offset_in_bytes, rdi_temp)));
 158   Register rdx_adapter = rdx_temp;
 159   // __ load_heap_oop(rdx_adapter, Address(rdx_temp, java_dyn_MethodTypeForm::genericInvoker_offset_in_bytes()));
 160   // deal with old JDK versions:
 161   __ lea(rdi_temp, Address(rdx_temp, __ delayed_value(java_dyn_MethodTypeForm::genericInvoker_offset_in_bytes, rdi_temp)));
 162   __ cmpptr(rdi_temp, rdx_temp);
 163   Label sorry_no_invoke_generic;
 164   __ jcc(Assembler::below, sorry_no_invoke_generic);
 165 
 166   __ load_heap_oop(rdx_adapter, Address(rdi_temp, 0));
 167   __ testptr(rdx_adapter, rdx_adapter);
 168   __ jcc(Assembler::zero, sorry_no_invoke_generic);
 169   __ movptr(Address(rcx_argslot, 1 * Interpreter::stackElementSize), rdx_adapter);
 170   // As a trusted first argument, pass the type being called, so the adapter knows
 171   // the actual types of the arguments and return values.
 172   // (Generic invokers are shared among form-families of method-type.)
 173   __ movptr(Address(rcx_argslot, 0 * Interpreter::stackElementSize), rax_mtype);
 174   // FIXME: assert that rdx_adapter is of the right method-type.
 175   __ mov(rcx, rdx_adapter);
 176   trace_method_handle(_masm, "invokeGeneric");
 177   __ jump_to_method_handle_entry(rcx, rdi_temp);
 178 
 179   __ bind(sorry_no_invoke_generic); // no invokeGeneric implementation available!
 180   __ movptr(rcx_recv, Address(rcx_argslot, -1 * Interpreter::stackElementSize));  // recover original MH
 181   __ push(rax_mtype);       // required mtype
 182   __ push(rcx_recv);        // bad mh (1st stacked argument)
 183   __ jump(ExternalAddress(Interpreter::throw_WrongMethodType_entry()));
 184 
 185   return entry_point;
 186 }
 187 
 188 // Helper to insert argument slots into the stack.
 189 // arg_slots must be a multiple of stack_move_unit() and <= 0
 190 void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
 191                                      RegisterOrConstant arg_slots,
 192                                      int arg_mask,
 193                                      Register rax_argslot,
 194                                      Register rbx_temp, Register rdx_temp, Register temp3_reg) {
 195   assert(temp3_reg == noreg, "temp3 not required");
 196   assert_different_registers(rax_argslot, rbx_temp, rdx_temp,
 197                              (!arg_slots.is_register() ? rsp : arg_slots.as_register()));
 198 
 199 #ifdef ASSERT
 200   verify_argslot(_masm, rax_argslot, "insertion point must fall within current frame");
 201   if (arg_slots.is_register()) {
 202     Label L_ok, L_bad;
 203     __ cmpptr(arg_slots.as_register(), (int32_t) NULL_WORD);
 204     __ jccb(Assembler::greater, L_bad);
 205     __ testl(arg_slots.as_register(), -stack_move_unit() - 1);
 206     __ jccb(Assembler::zero, L_ok);
 207     __ bind(L_bad);
 208     __ stop("assert arg_slots <= 0 and clear low bits");
 209     __ bind(L_ok);
 210   } else {
 211     assert(arg_slots.as_constant() <= 0, "");
 212     assert(arg_slots.as_constant() % -stack_move_unit() == 0, "");
 213   }
 214 #endif //ASSERT
 215 
 216 #ifdef _LP64
 217   if (arg_slots.is_register()) {
 218     // clean high bits of stack motion register (was loaded as an int)
 219     __ movslq(arg_slots.as_register(), arg_slots.as_register());
 220   }
 221 #endif
 222 
 223   // Make space on the stack for the inserted argument(s).
 224   // Then pull down everything shallower than rax_argslot.
 225   // The stacked return address gets pulled down with everything else.
 226   // That is, copy [rsp, argslot) downward by -size words.  In pseudo-code:
 227   //   rsp -= size;
 228   //   for (rdx = rsp + size; rdx < argslot; rdx++)
 229   //     rdx[-size] = rdx[0]
 230   //   argslot -= size;
 231   BLOCK_COMMENT("insert_arg_slots {");
 232   __ mov(rdx_temp, rsp);                        // source pointer for copy
 233   __ lea(rsp, Address(rsp, arg_slots, Address::times_ptr));
 234   {
 235     Label loop;
 236     __ BIND(loop);
 237     // pull one word down each time through the loop
 238     __ movptr(rbx_temp, Address(rdx_temp, 0));
 239     __ movptr(Address(rdx_temp, arg_slots, Address::times_ptr), rbx_temp);
 240     __ addptr(rdx_temp, wordSize);
 241     __ cmpptr(rdx_temp, rax_argslot);
 242     __ jccb(Assembler::less, loop);
 243   }
 244 
 245   // Now move the argslot down, to point to the opened-up space.
 246   __ lea(rax_argslot, Address(rax_argslot, arg_slots, Address::times_ptr));
 247   BLOCK_COMMENT("} insert_arg_slots");
 248 }
 249 
 250 // Helper to remove argument slots from the stack.
 251 // arg_slots must be a multiple of stack_move_unit() and >= 0
 252 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
 253                                     RegisterOrConstant arg_slots,
 254                                     Register rax_argslot,
 255                                      Register rbx_temp, Register rdx_temp, Register temp3_reg) {
 256   assert(temp3_reg == noreg, "temp3 not required");
 257   assert_different_registers(rax_argslot, rbx_temp, rdx_temp,
 258                              (!arg_slots.is_register() ? rsp : arg_slots.as_register()));
 259 
 260 #ifdef ASSERT
 261   // Verify that [argslot..argslot+size) lies within (rsp, rbp).
 262   __ lea(rbx_temp, Address(rax_argslot, arg_slots, Address::times_ptr));
 263   verify_argslot(_masm, rbx_temp, "deleted argument(s) must fall within current frame");
 264   if (arg_slots.is_register()) {
 265     Label L_ok, L_bad;
 266     __ cmpptr(arg_slots.as_register(), (int32_t) NULL_WORD);
 267     __ jccb(Assembler::less, L_bad);
 268     __ testl(arg_slots.as_register(), -stack_move_unit() - 1);
 269     __ jccb(Assembler::zero, L_ok);
 270     __ bind(L_bad);
 271     __ stop("assert arg_slots >= 0 and clear low bits");
 272     __ bind(L_ok);
 273   } else {
 274     assert(arg_slots.as_constant() >= 0, "");
 275     assert(arg_slots.as_constant() % -stack_move_unit() == 0, "");
 276   }
 277 #endif //ASSERT
 278 
 279 #ifdef _LP64
 280   if (false) {                  // not needed, since register is positive
 281     // clean high bits of stack motion register (was loaded as an int)
 282     if (arg_slots.is_register())
 283       __ movslq(arg_slots.as_register(), arg_slots.as_register());
 284   }
 285 #endif
 286 
 287   BLOCK_COMMENT("remove_arg_slots {");
 288   // Pull up everything shallower than rax_argslot.
 289   // Then remove the excess space on the stack.
 290   // The stacked return address gets pulled up with everything else.
 291   // That is, copy [rsp, argslot) upward by size words.  In pseudo-code:
 292   //   for (rdx = argslot-1; rdx >= rsp; --rdx)
 293   //     rdx[size] = rdx[0]
 294   //   argslot += size;
 295   //   rsp += size;
 296   __ lea(rdx_temp, Address(rax_argslot, -wordSize)); // source pointer for copy
 297   {
 298     Label loop;
 299     __ BIND(loop);
 300     // pull one word up each time through the loop
 301     __ movptr(rbx_temp, Address(rdx_temp, 0));
 302     __ movptr(Address(rdx_temp, arg_slots, Address::times_ptr), rbx_temp);
 303     __ addptr(rdx_temp, -wordSize);
 304     __ cmpptr(rdx_temp, rsp);
 305     __ jccb(Assembler::greaterEqual, loop);
 306   }
 307 
 308   // Now move the argslot up, to point to the just-copied block.
 309   __ lea(rsp, Address(rsp, arg_slots, Address::times_ptr));
 310   // And adjust the argslot address to point at the deletion point.
 311   __ lea(rax_argslot, Address(rax_argslot, arg_slots, Address::times_ptr));
 312   BLOCK_COMMENT("} remove_arg_slots");
 313 }
 314 
 315 #ifndef PRODUCT
 316 extern "C" void print_method_handle(oop mh);
 317 void trace_method_handle_stub(const char* adaptername,
 318                               oop mh,
 319                               intptr_t* saved_regs,
 320                               intptr_t* entry_sp,
 321                               intptr_t* saved_sp,
 322                               intptr_t* saved_bp) {
 323   // called as a leaf from native code: do not block the JVM!
 324   intptr_t* last_sp = (intptr_t*) saved_bp[frame::interpreter_frame_last_sp_offset];
 325   intptr_t* base_sp = (intptr_t*) saved_bp[frame::interpreter_frame_monitor_block_top_offset];
 326   printf("MH %s mh="INTPTR_FORMAT" sp=("INTPTR_FORMAT"+"INTX_FORMAT") stack_size="INTX_FORMAT" bp="INTPTR_FORMAT"\n",
 327          adaptername, (intptr_t)mh, (intptr_t)entry_sp, (intptr_t)(saved_sp - entry_sp), (intptr_t)(base_sp - last_sp), (intptr_t)saved_bp);
 328   if (last_sp != saved_sp && last_sp != NULL)
 329     printf("*** last_sp="INTPTR_FORMAT"\n", (intptr_t)last_sp);
 330   if (Verbose) {
 331     printf(" reg dump: ");
 332     int saved_regs_count = (entry_sp-1) - saved_regs;
 333     // 32 bit: rdi rsi rbp rsp; rbx rdx rcx (*) rax
 334     int i;
 335     for (i = 0; i <= saved_regs_count; i++) {
 336       if (i > 0 && i % 4 == 0 && i != saved_regs_count)
 337         printf("\n   + dump: ");
 338       printf(" %d: "INTPTR_FORMAT, i, saved_regs[i]);
 339     }
 340     printf("\n");
 341     int stack_dump_count = 16;
 342     if (stack_dump_count < (int)(saved_bp + 2 - saved_sp))
 343       stack_dump_count = (int)(saved_bp + 2 - saved_sp);
 344     if (stack_dump_count > 64)  stack_dump_count = 48;
 345     for (i = 0; i < stack_dump_count; i += 4) {
 346       printf(" dump at SP[%d] "INTPTR_FORMAT": "INTPTR_FORMAT" "INTPTR_FORMAT" "INTPTR_FORMAT" "INTPTR_FORMAT"\n",
 347              i, (intptr_t) &entry_sp[i+0], entry_sp[i+0], entry_sp[i+1], entry_sp[i+2], entry_sp[i+3]);
 348     }
 349     print_method_handle(mh);
 350   }
 351 }
 352 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 353   if (!TraceMethodHandles)  return;
 354   BLOCK_COMMENT("trace_method_handle {");
 355   __ push(rax);
 356   __ lea(rax, Address(rsp, wordSize*6)); // entry_sp
 357   __ pusha();
 358   // arguments:
 359   __ push(rbp);               // interpreter frame pointer
 360   __ push(rsi);               // saved_sp
 361   __ push(rax);               // entry_sp
 362   __ push(rcx);               // mh
 363   __ push(rcx);
 364   __ movptr(Address(rsp, 0), (intptr_t) adaptername);
 365   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub), 5);
 366   __ popa();
 367   __ pop(rax);
 368   BLOCK_COMMENT("} trace_method_handle");
 369 }
 370 #endif //PRODUCT
 371 
 372 // which conversion op types are implemented here?
 373 int MethodHandles::adapter_conversion_ops_supported_mask() {
 374   return ((1<<sun_dyn_AdapterMethodHandle::OP_RETYPE_ONLY)
 375          |(1<<sun_dyn_AdapterMethodHandle::OP_RETYPE_RAW)
 376          |(1<<sun_dyn_AdapterMethodHandle::OP_CHECK_CAST)
 377          |(1<<sun_dyn_AdapterMethodHandle::OP_PRIM_TO_PRIM)
 378          |(1<<sun_dyn_AdapterMethodHandle::OP_REF_TO_PRIM)
 379          |(1<<sun_dyn_AdapterMethodHandle::OP_SWAP_ARGS)
 380          |(1<<sun_dyn_AdapterMethodHandle::OP_ROT_ARGS)
 381          |(1<<sun_dyn_AdapterMethodHandle::OP_DUP_ARGS)
 382          |(1<<sun_dyn_AdapterMethodHandle::OP_DROP_ARGS)
 383          //|(1<<sun_dyn_AdapterMethodHandle::OP_SPREAD_ARGS) //BUG!
 384          );
 385   // FIXME: MethodHandlesTest gets a crash if we enable OP_SPREAD_ARGS.
 386 }
 387 
 388 //------------------------------------------------------------------------------
 389 // MethodHandles::generate_method_handle_stub
 390 //
 391 // Generate an "entry" field for a method handle.
 392 // This determines how the method handle will respond to calls.
 393 void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek, TRAPS) {
 394   // Here is the register state during an interpreted call,
 395   // as set up by generate_method_handle_interpreter_entry():
 396   // - rbx: garbage temp (was MethodHandle.invoke methodOop, unused)
 397   // - rcx: receiver method handle
 398   // - rax: method handle type (only used by the check_mtype entry point)
 399   // - rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
 400   // - rdx: garbage temp, can blow away
 401 
 402   const Register rcx_recv    = rcx;
 403   const Register rax_argslot = rax;
 404   const Register rbx_temp    = rbx;
 405   const Register rdx_temp    = rdx;
 406 
 407   // This guy is set up by prepare_to_jump_from_interpreted (from interpreted calls)
 408   // and gen_c2i_adapter (from compiled calls):
 409   const Register saved_last_sp = LP64_ONLY(r13) NOT_LP64(rsi);
 410 
 411   // Argument registers for _raise_exception.
 412   // 32-bit: Pass first two oop/int args in registers ECX and EDX.
 413   const Register rarg0_code     = LP64_ONLY(j_rarg0) NOT_LP64(rcx);
 414   const Register rarg1_actual   = LP64_ONLY(j_rarg1) NOT_LP64(rdx);
 415   const Register rarg2_required = LP64_ONLY(j_rarg2) NOT_LP64(rdi);
 416   assert_different_registers(rarg0_code, rarg1_actual, rarg2_required, saved_last_sp);
 417 
 418   guarantee(java_dyn_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
 419 
 420   // some handy addresses
 421   Address rbx_method_fie(     rbx,      methodOopDesc::from_interpreted_offset() );
 422 
 423   Address rcx_mh_vmtarget(    rcx_recv, java_dyn_MethodHandle::vmtarget_offset_in_bytes() );
 424   Address rcx_dmh_vmindex(    rcx_recv, sun_dyn_DirectMethodHandle::vmindex_offset_in_bytes() );
 425 
 426   Address rcx_bmh_vmargslot(  rcx_recv, sun_dyn_BoundMethodHandle::vmargslot_offset_in_bytes() );
 427   Address rcx_bmh_argument(   rcx_recv, sun_dyn_BoundMethodHandle::argument_offset_in_bytes() );
 428 
 429   Address rcx_amh_vmargslot(  rcx_recv, sun_dyn_AdapterMethodHandle::vmargslot_offset_in_bytes() );
 430   Address rcx_amh_argument(   rcx_recv, sun_dyn_AdapterMethodHandle::argument_offset_in_bytes() );
 431   Address rcx_amh_conversion( rcx_recv, sun_dyn_AdapterMethodHandle::conversion_offset_in_bytes() );
 432   Address vmarg;                // __ argument_address(vmargslot)
 433 
 434   const int java_mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 435 
 436   if (have_entry(ek)) {
 437     __ nop();                   // empty stubs make SG sick
 438     return;
 439   }
 440 
 441   address interp_entry = __ pc();
 442 
 443   trace_method_handle(_masm, entry_name(ek));
 444 
 445   BLOCK_COMMENT(entry_name(ek));
 446 
 447   switch ((int) ek) {
 448   case _raise_exception:
 449     {
 450       // Not a real MH entry, but rather shared code for raising an
 451       // exception.  Since we use a C2I adapter to set up the
 452       // interpreter state, arguments are expected in compiler
 453       // argument registers.
 454       methodHandle mh(raise_exception_method());
 455       address c2i_entry = methodOopDesc::make_adapters(mh, CHECK);

 456 
 457       const Register rdi_pc = rax;
 458       __ pop(rdi_pc);  // caller PC
 459       __ mov(rsp, saved_last_sp);  // cut the stack back to where the caller started
 460 
 461       Register rbx_method = rbx_temp;
 462       Label L_no_method;
 463       // FIXME: fill in _raise_exception_method with a suitable sun.dyn method
 464       __ movptr(rbx_method, ExternalAddress((address) &_raise_exception_method));
 465       __ testptr(rbx_method, rbx_method);
 466       __ jccb(Assembler::zero, L_no_method);
 467 
 468       const int jobject_oop_offset = 0;
 469       __ movptr(rbx_method, Address(rbx_method, jobject_oop_offset));  // dereference the jobject
 470       __ testptr(rbx_method, rbx_method);
 471       __ jccb(Assembler::zero, L_no_method);
 472       __ verify_oop(rbx_method);
 473 
 474       // 32-bit: push remaining arguments as if coming from the compiler.
 475       NOT_LP64(__ push(rarg2_required));
 476 
 477       __ push(rdi_pc);  // restore caller PC
 478       __ jump(ExternalAddress(c2i_entry));  // do C2I transition
 479 
 480       // If we get here, the Java runtime did not do its job of creating the exception.
 481       // Do something that is at least causes a valid throw from the interpreter.
 482       __ bind(L_no_method);
 483       __ push(rarg2_required);
 484       __ push(rarg1_actual);
 485       __ jump(ExternalAddress(Interpreter::throw_WrongMethodType_entry()));
 486     }
 487     break;
 488 
 489   case _invokestatic_mh:
 490   case _invokespecial_mh:
 491     {
 492       Register rbx_method = rbx_temp;
 493       __ load_heap_oop(rbx_method, rcx_mh_vmtarget); // target is a methodOop
 494       __ verify_oop(rbx_method);
 495       // same as TemplateTable::invokestatic or invokespecial,
 496       // minus the CP setup and profiling:
 497       if (ek == _invokespecial_mh) {
 498         // Must load & check the first argument before entering the target method.
 499         __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
 500         __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
 501         __ null_check(rcx_recv);
 502         __ verify_oop(rcx_recv);
 503       }
 504       __ jmp(rbx_method_fie);
 505     }
 506     break;
 507 
 508   case _invokevirtual_mh:
 509     {
 510       // same as TemplateTable::invokevirtual,
 511       // minus the CP setup and profiling:
 512 
 513       // pick out the vtable index and receiver offset from the MH,
 514       // and then we can discard it:
 515       __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
 516       Register rbx_index = rbx_temp;
 517       __ movl(rbx_index, rcx_dmh_vmindex);
 518       // Note:  The verifier allows us to ignore rcx_mh_vmtarget.
 519       __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
 520       __ null_check(rcx_recv, oopDesc::klass_offset_in_bytes());
 521 
 522       // get receiver klass
 523       Register rax_klass = rax_argslot;
 524       __ load_klass(rax_klass, rcx_recv);
 525       __ verify_oop(rax_klass);
 526 
 527       // get target methodOop & entry point
 528       const int base = instanceKlass::vtable_start_offset() * wordSize;
 529       assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
 530       Address vtable_entry_addr(rax_klass,
 531                                 rbx_index, Address::times_ptr,
 532                                 base + vtableEntry::method_offset_in_bytes());
 533       Register rbx_method = rbx_temp;
 534       __ movptr(rbx_method, vtable_entry_addr);
 535 
 536       __ verify_oop(rbx_method);
 537       __ jmp(rbx_method_fie);
 538     }
 539     break;
 540 
 541   case _invokeinterface_mh:
 542     {
 543       // same as TemplateTable::invokeinterface,
 544       // minus the CP setup and profiling:
 545 
 546       // pick out the interface and itable index from the MH.
 547       __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
 548       Register rdx_intf  = rdx_temp;
 549       Register rbx_index = rbx_temp;
 550       __ load_heap_oop(rdx_intf, rcx_mh_vmtarget);
 551       __ movl(rbx_index, rcx_dmh_vmindex);
 552       __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
 553       __ null_check(rcx_recv, oopDesc::klass_offset_in_bytes());
 554 
 555       // get receiver klass
 556       Register rax_klass = rax_argslot;
 557       __ load_klass(rax_klass, rcx_recv);
 558       __ verify_oop(rax_klass);
 559 
 560       Register rdi_temp   = rdi;
 561       Register rbx_method = rbx_index;
 562 
 563       // get interface klass
 564       Label no_such_interface;
 565       __ verify_oop(rdx_intf);
 566       __ lookup_interface_method(rax_klass, rdx_intf,
 567                                  // note: next two args must be the same:
 568                                  rbx_index, rbx_method,
 569                                  rdi_temp,
 570                                  no_such_interface);
 571 
 572       __ verify_oop(rbx_method);
 573       __ jmp(rbx_method_fie);
 574       __ hlt();
 575 
 576       __ bind(no_such_interface);
 577       // Throw an exception.
 578       // For historical reasons, it will be IncompatibleClassChangeError.
 579       __ mov(rbx_temp, rcx_recv);  // rarg2_required might be RCX
 580       assert_different_registers(rarg2_required, rbx_temp);
 581       __ movptr(rarg2_required, Address(rdx_intf, java_mirror_offset));  // required interface
 582       __ mov(   rarg1_actual,   rbx_temp);                               // bad receiver
 583       __ movl(  rarg0_code,     (int) Bytecodes::_invokeinterface);      // who is complaining?
 584       __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
 585     }
 586     break;
 587 
 588   case _bound_ref_mh:
 589   case _bound_int_mh:
 590   case _bound_long_mh:
 591   case _bound_ref_direct_mh:
 592   case _bound_int_direct_mh:
 593   case _bound_long_direct_mh:
 594     {
 595       bool direct_to_method = (ek >= _bound_ref_direct_mh);
 596       BasicType arg_type  = T_ILLEGAL;
 597       int       arg_mask  = _INSERT_NO_MASK;
 598       int       arg_slots = -1;
 599       get_ek_bound_mh_info(ek, arg_type, arg_mask, arg_slots);
 600 
 601       // make room for the new argument:
 602       __ movl(rax_argslot, rcx_bmh_vmargslot);
 603       __ lea(rax_argslot, __ argument_address(rax_argslot));
 604       insert_arg_slots(_masm, arg_slots * stack_move_unit(), arg_mask,
 605                        rax_argslot, rbx_temp, rdx_temp);
 606 
 607       // store bound argument into the new stack slot:
 608       __ load_heap_oop(rbx_temp, rcx_bmh_argument);
 609       Address prim_value_addr(rbx_temp, java_lang_boxing_object::value_offset_in_bytes(arg_type));
 610       if (arg_type == T_OBJECT) {
 611         __ movptr(Address(rax_argslot, 0), rbx_temp);
 612       } else {
 613         __ load_sized_value(rdx_temp, prim_value_addr,
 614                             type2aelembytes(arg_type), is_signed_subword_type(arg_type));
 615         __ movptr(Address(rax_argslot, 0), rdx_temp);
 616 #ifndef _LP64
 617         if (arg_slots == 2) {
 618           __ movl(rdx_temp, prim_value_addr.plus_disp(wordSize));
 619           __ movl(Address(rax_argslot, Interpreter::stackElementSize), rdx_temp);
 620         }
 621 #endif //_LP64
 622       }
 623 
 624       if (direct_to_method) {
 625         Register rbx_method = rbx_temp;
 626         __ load_heap_oop(rbx_method, rcx_mh_vmtarget);
 627         __ verify_oop(rbx_method);
 628         __ jmp(rbx_method_fie);
 629       } else {
 630         __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 631         __ verify_oop(rcx_recv);
 632         __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 633       }
 634     }
 635     break;
 636 
 637   case _adapter_retype_only:
 638   case _adapter_retype_raw:
 639     // immediately jump to the next MH layer:
 640     __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 641     __ verify_oop(rcx_recv);
 642     __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 643     // This is OK when all parameter types widen.
 644     // It is also OK when a return type narrows.
 645     break;
 646 
 647   case _adapter_check_cast:
 648     {
 649       // temps:
 650       Register rbx_klass = rbx_temp; // interesting AMH data
 651 
 652       // check a reference argument before jumping to the next layer of MH:
 653       __ movl(rax_argslot, rcx_amh_vmargslot);
 654       vmarg = __ argument_address(rax_argslot);
 655 
 656       // What class are we casting to?
 657       __ load_heap_oop(rbx_klass, rcx_amh_argument); // this is a Class object!
 658       __ load_heap_oop(rbx_klass, Address(rbx_klass, java_lang_Class::klass_offset_in_bytes()));
 659 
 660       Label done;
 661       __ movptr(rdx_temp, vmarg);
 662       __ testptr(rdx_temp, rdx_temp);
 663       __ jcc(Assembler::zero, done);         // no cast if null
 664       __ load_klass(rdx_temp, rdx_temp);
 665 
 666       // live at this point:
 667       // - rbx_klass:  klass required by the target method
 668       // - rdx_temp:   argument klass to test
 669       // - rcx_recv:   adapter method handle
 670       __ check_klass_subtype(rdx_temp, rbx_klass, rax_argslot, done);
 671 
 672       // If we get here, the type check failed!
 673       // Call the wrong_method_type stub, passing the failing argument type in rax.
 674       Register rax_mtype = rax_argslot;
 675       __ movl(rax_argslot, rcx_amh_vmargslot);  // reload argslot field
 676       __ movptr(rdx_temp, vmarg);
 677 
 678       assert_different_registers(rarg2_required, rdx_temp);
 679       __ load_heap_oop(rarg2_required, rcx_amh_argument);             // required class
 680       __ mov(          rarg1_actual,   rdx_temp);                     // bad object
 681       __ movl(         rarg0_code,     (int) Bytecodes::_checkcast);  // who is complaining?
 682       __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
 683 
 684       __ bind(done);
 685       // get the new MH:
 686       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 687       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 688     }
 689     break;
 690 
 691   case _adapter_prim_to_prim:
 692   case _adapter_ref_to_prim:
 693     // handled completely by optimized cases
 694     __ stop("init_AdapterMethodHandle should not issue this");
 695     break;
 696 
 697   case _adapter_opt_i2i:        // optimized subcase of adapt_prim_to_prim
 698 //case _adapter_opt_f2i:        // optimized subcase of adapt_prim_to_prim
 699   case _adapter_opt_l2i:        // optimized subcase of adapt_prim_to_prim
 700   case _adapter_opt_unboxi:     // optimized subcase of adapt_ref_to_prim
 701     {
 702       // perform an in-place conversion to int or an int subword
 703       __ movl(rax_argslot, rcx_amh_vmargslot);
 704       vmarg = __ argument_address(rax_argslot);
 705 
 706       switch (ek) {
 707       case _adapter_opt_i2i:
 708         __ movl(rdx_temp, vmarg);
 709         break;
 710       case _adapter_opt_l2i:
 711         {
 712           // just delete the extra slot; on a little-endian machine we keep the first
 713           __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
 714           remove_arg_slots(_masm, -stack_move_unit(),
 715                            rax_argslot, rbx_temp, rdx_temp);
 716           vmarg = Address(rax_argslot, -Interpreter::stackElementSize);
 717           __ movl(rdx_temp, vmarg);
 718         }
 719         break;
 720       case _adapter_opt_unboxi:
 721         {
 722           // Load the value up from the heap.
 723           __ movptr(rdx_temp, vmarg);
 724           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
 725 #ifdef ASSERT
 726           for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
 727             if (is_subword_type(BasicType(bt)))
 728               assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
 729           }
 730 #endif
 731           __ null_check(rdx_temp, value_offset);
 732           __ movl(rdx_temp, Address(rdx_temp, value_offset));
 733           // We load this as a word.  Because we are little-endian,
 734           // the low bits will be correct, but the high bits may need cleaning.
 735           // The vminfo will guide us to clean those bits.
 736         }
 737         break;
 738       default:
 739         ShouldNotReachHere();
 740       }
 741 
 742       // Do the requested conversion and store the value.
 743       Register rbx_vminfo = rbx_temp;
 744       __ movl(rbx_vminfo, rcx_amh_conversion);
 745       assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 746 
 747       // get the new MH:
 748       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 749       // (now we are done with the old MH)
 750 
 751       // original 32-bit vmdata word must be of this form:
 752       //    | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
 753       __ xchgptr(rcx, rbx_vminfo);                // free rcx for shifts
 754       __ shll(rdx_temp /*, rcx*/);
 755       Label zero_extend, done;
 756       __ testl(rcx, CONV_VMINFO_SIGN_FLAG);
 757       __ jccb(Assembler::zero, zero_extend);
 758 
 759       // this path is taken for int->byte, int->short
 760       __ sarl(rdx_temp /*, rcx*/);
 761       __ jmpb(done);
 762 
 763       __ bind(zero_extend);
 764       // this is taken for int->char
 765       __ shrl(rdx_temp /*, rcx*/);
 766 
 767       __ bind(done);
 768       __ movl(vmarg, rdx_temp);  // Store the value.
 769       __ xchgptr(rcx, rbx_vminfo);                // restore rcx_recv
 770 
 771       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 772     }
 773     break;
 774 
 775   case _adapter_opt_i2l:        // optimized subcase of adapt_prim_to_prim
 776   case _adapter_opt_unboxl:     // optimized subcase of adapt_ref_to_prim
 777     {
 778       // perform an in-place int-to-long or ref-to-long conversion
 779       __ movl(rax_argslot, rcx_amh_vmargslot);
 780 
 781       // on a little-endian machine we keep the first slot and add another after
 782       __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
 783       insert_arg_slots(_masm, stack_move_unit(), _INSERT_INT_MASK,
 784                        rax_argslot, rbx_temp, rdx_temp);
 785       Address vmarg1(rax_argslot, -Interpreter::stackElementSize);
 786       Address vmarg2 = vmarg1.plus_disp(Interpreter::stackElementSize);
 787 
 788       switch (ek) {
 789       case _adapter_opt_i2l:
 790         {
 791 #ifdef _LP64
 792           __ movslq(rdx_temp, vmarg1);  // Load sign-extended
 793           __ movq(vmarg1, rdx_temp);    // Store into first slot
 794 #else
 795           __ movl(rdx_temp, vmarg1);
 796           __ sarl(rdx_temp, BitsPerInt - 1);  // __ extend_sign()
 797           __ movl(vmarg2, rdx_temp); // store second word
 798 #endif
 799         }
 800         break;
 801       case _adapter_opt_unboxl:
 802         {
 803           // Load the value up from the heap.
 804           __ movptr(rdx_temp, vmarg1);
 805           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
 806           assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
 807           __ null_check(rdx_temp, value_offset);
 808 #ifdef _LP64
 809           __ movq(rbx_temp, Address(rdx_temp, value_offset));
 810           __ movq(vmarg1, rbx_temp);
 811 #else
 812           __ movl(rbx_temp, Address(rdx_temp, value_offset + 0*BytesPerInt));
 813           __ movl(rdx_temp, Address(rdx_temp, value_offset + 1*BytesPerInt));
 814           __ movl(vmarg1, rbx_temp);
 815           __ movl(vmarg2, rdx_temp);
 816 #endif
 817         }
 818         break;
 819       default:
 820         ShouldNotReachHere();
 821       }
 822 
 823       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 824       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 825     }
 826     break;
 827 
 828   case _adapter_opt_f2d:        // optimized subcase of adapt_prim_to_prim
 829   case _adapter_opt_d2f:        // optimized subcase of adapt_prim_to_prim
 830     {
 831       // perform an in-place floating primitive conversion
 832       __ movl(rax_argslot, rcx_amh_vmargslot);
 833       __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
 834       if (ek == _adapter_opt_f2d) {
 835         insert_arg_slots(_masm, stack_move_unit(), _INSERT_INT_MASK,
 836                          rax_argslot, rbx_temp, rdx_temp);
 837       }
 838       Address vmarg(rax_argslot, -Interpreter::stackElementSize);
 839 
 840 #ifdef _LP64
 841       if (ek == _adapter_opt_f2d) {
 842         __ movflt(xmm0, vmarg);
 843         __ cvtss2sd(xmm0, xmm0);
 844         __ movdbl(vmarg, xmm0);
 845       } else {
 846         __ movdbl(xmm0, vmarg);
 847         __ cvtsd2ss(xmm0, xmm0);
 848         __ movflt(vmarg, xmm0);
 849       }
 850 #else //_LP64
 851       if (ek == _adapter_opt_f2d) {
 852         __ fld_s(vmarg);        // load float to ST0
 853         __ fstp_s(vmarg);       // store single
 854       } else {
 855         __ fld_d(vmarg);        // load double to ST0
 856         __ fstp_s(vmarg);       // store single
 857       }
 858 #endif //_LP64
 859 
 860       if (ek == _adapter_opt_d2f) {
 861         remove_arg_slots(_masm, -stack_move_unit(),
 862                          rax_argslot, rbx_temp, rdx_temp);
 863       }
 864 
 865       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 866       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 867     }
 868     break;
 869 
 870   case _adapter_prim_to_ref:
 871     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
 872     break;
 873 
 874   case _adapter_swap_args:
 875   case _adapter_rot_args:
 876     // handled completely by optimized cases
 877     __ stop("init_AdapterMethodHandle should not issue this");
 878     break;
 879 
 880   case _adapter_opt_swap_1:
 881   case _adapter_opt_swap_2:
 882   case _adapter_opt_rot_1_up:
 883   case _adapter_opt_rot_1_down:
 884   case _adapter_opt_rot_2_up:
 885   case _adapter_opt_rot_2_down:
 886     {
 887       int swap_bytes = 0, rotate = 0;
 888       get_ek_adapter_opt_swap_rot_info(ek, swap_bytes, rotate);
 889 
 890       // 'argslot' is the position of the first argument to swap
 891       __ movl(rax_argslot, rcx_amh_vmargslot);
 892       __ lea(rax_argslot, __ argument_address(rax_argslot));
 893 
 894       // 'vminfo' is the second
 895       Register rbx_destslot = rbx_temp;
 896       __ movl(rbx_destslot, rcx_amh_conversion);
 897       assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 898       __ andl(rbx_destslot, CONV_VMINFO_MASK);
 899       __ lea(rbx_destslot, __ argument_address(rbx_destslot));
 900       DEBUG_ONLY(verify_argslot(_masm, rbx_destslot, "swap point must fall within current frame"));
 901 
 902       if (!rotate) {
 903         for (int i = 0; i < swap_bytes; i += wordSize) {
 904           __ movptr(rdx_temp, Address(rax_argslot , i));
 905           __ push(rdx_temp);
 906           __ movptr(rdx_temp, Address(rbx_destslot, i));
 907           __ movptr(Address(rax_argslot, i), rdx_temp);
 908           __ pop(rdx_temp);
 909           __ movptr(Address(rbx_destslot, i), rdx_temp);
 910         }
 911       } else {
 912         // push the first chunk, which is going to get overwritten
 913         for (int i = swap_bytes; (i -= wordSize) >= 0; ) {
 914           __ movptr(rdx_temp, Address(rax_argslot, i));
 915           __ push(rdx_temp);
 916         }
 917 
 918         if (rotate > 0) {
 919           // rotate upward
 920           __ subptr(rax_argslot, swap_bytes);
 921 #ifdef ASSERT
 922           {
 923             // Verify that argslot > destslot, by at least swap_bytes.
 924             Label L_ok;
 925             __ cmpptr(rax_argslot, rbx_destslot);
 926             __ jccb(Assembler::aboveEqual, L_ok);
 927             __ stop("source must be above destination (upward rotation)");
 928             __ bind(L_ok);
 929           }
 930 #endif
 931           // work argslot down to destslot, copying contiguous data upwards
 932           // pseudo-code:
 933           //   rax = src_addr - swap_bytes
 934           //   rbx = dest_addr
 935           //   while (rax >= rbx) *(rax + swap_bytes) = *(rax + 0), rax--;
 936           Label loop;
 937           __ bind(loop);
 938           __ movptr(rdx_temp, Address(rax_argslot, 0));
 939           __ movptr(Address(rax_argslot, swap_bytes), rdx_temp);
 940           __ addptr(rax_argslot, -wordSize);
 941           __ cmpptr(rax_argslot, rbx_destslot);
 942           __ jccb(Assembler::aboveEqual, loop);
 943         } else {
 944           __ addptr(rax_argslot, swap_bytes);
 945 #ifdef ASSERT
 946           {
 947             // Verify that argslot < destslot, by at least swap_bytes.
 948             Label L_ok;
 949             __ cmpptr(rax_argslot, rbx_destslot);
 950             __ jccb(Assembler::belowEqual, L_ok);
 951             __ stop("source must be below destination (downward rotation)");
 952             __ bind(L_ok);
 953           }
 954 #endif
 955           // work argslot up to destslot, copying contiguous data downwards
 956           // pseudo-code:
 957           //   rax = src_addr + swap_bytes
 958           //   rbx = dest_addr
 959           //   while (rax <= rbx) *(rax - swap_bytes) = *(rax + 0), rax++;
 960           Label loop;
 961           __ bind(loop);
 962           __ movptr(rdx_temp, Address(rax_argslot, 0));
 963           __ movptr(Address(rax_argslot, -swap_bytes), rdx_temp);
 964           __ addptr(rax_argslot, wordSize);
 965           __ cmpptr(rax_argslot, rbx_destslot);
 966           __ jccb(Assembler::belowEqual, loop);
 967         }
 968 
 969         // pop the original first chunk into the destination slot, now free
 970         for (int i = 0; i < swap_bytes; i += wordSize) {
 971           __ pop(rdx_temp);
 972           __ movptr(Address(rbx_destslot, i), rdx_temp);
 973         }
 974       }
 975 
 976       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
 977       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
 978     }
 979     break;
 980 
 981   case _adapter_dup_args:
 982     {
 983       // 'argslot' is the position of the first argument to duplicate
 984       __ movl(rax_argslot, rcx_amh_vmargslot);
 985       __ lea(rax_argslot, __ argument_address(rax_argslot));
 986 
 987       // 'stack_move' is negative number of words to duplicate
 988       Register rdx_stack_move = rdx_temp;
 989       __ movl2ptr(rdx_stack_move, rcx_amh_conversion);
 990       __ sarptr(rdx_stack_move, CONV_STACK_MOVE_SHIFT);
 991 
 992       int argslot0_num = 0;
 993       Address argslot0 = __ argument_address(RegisterOrConstant(argslot0_num));
 994       assert(argslot0.base() == rsp, "");
 995       int pre_arg_size = argslot0.disp();
 996       assert(pre_arg_size % wordSize == 0, "");
 997       assert(pre_arg_size > 0, "must include PC");
 998 
 999       // remember the old rsp+1 (argslot[0])
1000       Register rbx_oldarg = rbx_temp;
1001       __ lea(rbx_oldarg, argslot0);
1002 
1003       // move rsp down to make room for dups
1004       __ lea(rsp, Address(rsp, rdx_stack_move, Address::times_ptr));
1005 
1006       // compute the new rsp+1 (argslot[0])
1007       Register rdx_newarg = rdx_temp;
1008       __ lea(rdx_newarg, argslot0);
1009 
1010       __ push(rdi);             // need a temp
1011       // (preceding push must be done after arg addresses are taken!)
1012 
1013       // pull down the pre_arg_size data (PC)
1014       for (int i = -pre_arg_size; i < 0; i += wordSize) {
1015         __ movptr(rdi, Address(rbx_oldarg, i));
1016         __ movptr(Address(rdx_newarg, i), rdi);
1017       }
1018 
1019       // copy from rax_argslot[0...] down to new_rsp[1...]
1020       // pseudo-code:
1021       //   rbx = old_rsp+1
1022       //   rdx = new_rsp+1
1023       //   rax = argslot
1024       //   while (rdx < rbx) *rdx++ = *rax++
1025       Label loop;
1026       __ bind(loop);
1027       __ movptr(rdi, Address(rax_argslot, 0));
1028       __ movptr(Address(rdx_newarg, 0), rdi);
1029       __ addptr(rax_argslot, wordSize);
1030       __ addptr(rdx_newarg, wordSize);
1031       __ cmpptr(rdx_newarg, rbx_oldarg);
1032       __ jccb(Assembler::less, loop);
1033 
1034       __ pop(rdi);              // restore temp
1035 
1036       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1037       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1038     }
1039     break;
1040 
1041   case _adapter_drop_args:
1042     {
1043       // 'argslot' is the position of the first argument to nuke
1044       __ movl(rax_argslot, rcx_amh_vmargslot);
1045       __ lea(rax_argslot, __ argument_address(rax_argslot));
1046 
1047       __ push(rdi);             // need a temp
1048       // (must do previous push after argslot address is taken)
1049 
1050       // 'stack_move' is number of words to drop
1051       Register rdi_stack_move = rdi;
1052       __ movl2ptr(rdi_stack_move, rcx_amh_conversion);
1053       __ sarptr(rdi_stack_move, CONV_STACK_MOVE_SHIFT);
1054       remove_arg_slots(_masm, rdi_stack_move,
1055                        rax_argslot, rbx_temp, rdx_temp);
1056 
1057       __ pop(rdi);              // restore temp
1058 
1059       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1060       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1061     }
1062     break;
1063 
1064   case _adapter_collect_args:
1065     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
1066     break;
1067 
1068   case _adapter_spread_args:
1069     // handled completely by optimized cases
1070     __ stop("init_AdapterMethodHandle should not issue this");
1071     break;
1072 
1073   case _adapter_opt_spread_0:
1074   case _adapter_opt_spread_1:
1075   case _adapter_opt_spread_more:
1076     {
1077       // spread an array out into a group of arguments
1078       int length_constant = get_ek_adapter_opt_spread_info(ek);
1079 
1080       // find the address of the array argument
1081       __ movl(rax_argslot, rcx_amh_vmargslot);
1082       __ lea(rax_argslot, __ argument_address(rax_argslot));
1083 
1084       // grab some temps
1085       { __ push(rsi); __ push(rdi); }
1086       // (preceding pushes must be done after argslot address is taken!)
1087 #define UNPUSH_RSI_RDI \
1088       { __ pop(rdi); __ pop(rsi); }
1089 
1090       // arx_argslot points both to the array and to the first output arg
1091       vmarg = Address(rax_argslot, 0);
1092 
1093       // Get the array value.
1094       Register  rsi_array       = rsi;
1095       Register  rdx_array_klass = rdx_temp;
1096       BasicType elem_type       = T_OBJECT;
1097       int       length_offset   = arrayOopDesc::length_offset_in_bytes();
1098       int       elem0_offset    = arrayOopDesc::base_offset_in_bytes(elem_type);
1099       __ movptr(rsi_array, vmarg);
1100       Label skip_array_check;
1101       if (length_constant == 0) {
1102         __ testptr(rsi_array, rsi_array);
1103         __ jcc(Assembler::zero, skip_array_check);
1104       }
1105       __ null_check(rsi_array, oopDesc::klass_offset_in_bytes());
1106       __ load_klass(rdx_array_klass, rsi_array);
1107 
1108       // Check the array type.
1109       Register rbx_klass = rbx_temp;
1110       __ load_heap_oop(rbx_klass, rcx_amh_argument); // this is a Class object!
1111       __ load_heap_oop(rbx_klass, Address(rbx_klass, java_lang_Class::klass_offset_in_bytes()));
1112 
1113       Label ok_array_klass, bad_array_klass, bad_array_length;
1114       __ check_klass_subtype(rdx_array_klass, rbx_klass, rdi, ok_array_klass);
1115       // If we get here, the type check failed!
1116       __ jmp(bad_array_klass);
1117       __ bind(ok_array_klass);
1118 
1119       // Check length.
1120       if (length_constant >= 0) {
1121         __ cmpl(Address(rsi_array, length_offset), length_constant);
1122       } else {
1123         Register rbx_vminfo = rbx_temp;
1124         __ movl(rbx_vminfo, rcx_amh_conversion);
1125         assert(CONV_VMINFO_SHIFT == 0, "preshifted");
1126         __ andl(rbx_vminfo, CONV_VMINFO_MASK);
1127         __ cmpl(rbx_vminfo, Address(rsi_array, length_offset));
1128       }
1129       __ jcc(Assembler::notEqual, bad_array_length);
1130 
1131       Register rdx_argslot_limit = rdx_temp;
1132 
1133       // Array length checks out.  Now insert any required stack slots.
1134       if (length_constant == -1) {
1135         // Form a pointer to the end of the affected region.
1136         __ lea(rdx_argslot_limit, Address(rax_argslot, Interpreter::stackElementSize));
1137         // 'stack_move' is negative number of words to insert
1138         Register rdi_stack_move = rdi;
1139         __ movl2ptr(rdi_stack_move, rcx_amh_conversion);
1140         __ sarptr(rdi_stack_move, CONV_STACK_MOVE_SHIFT);
1141         Register rsi_temp = rsi_array;  // spill this
1142         insert_arg_slots(_masm, rdi_stack_move, -1,
1143                          rax_argslot, rbx_temp, rsi_temp);
1144         // reload the array (since rsi was killed)
1145         __ movptr(rsi_array, vmarg);
1146       } else if (length_constant > 1) {
1147         int arg_mask = 0;
1148         int new_slots = (length_constant - 1);
1149         for (int i = 0; i < new_slots; i++) {
1150           arg_mask <<= 1;
1151           arg_mask |= _INSERT_REF_MASK;
1152         }
1153         insert_arg_slots(_masm, new_slots * stack_move_unit(), arg_mask,
1154                          rax_argslot, rbx_temp, rdx_temp);
1155       } else if (length_constant == 1) {
1156         // no stack resizing required
1157       } else if (length_constant == 0) {
1158         remove_arg_slots(_masm, -stack_move_unit(),
1159                          rax_argslot, rbx_temp, rdx_temp);
1160       }
1161 
1162       // Copy from the array to the new slots.
1163       // Note: Stack change code preserves integrity of rax_argslot pointer.
1164       // So even after slot insertions, rax_argslot still points to first argument.
1165       if (length_constant == -1) {
1166         // [rax_argslot, rdx_argslot_limit) is the area we are inserting into.
1167         Register rsi_source = rsi_array;
1168         __ lea(rsi_source, Address(rsi_array, elem0_offset));
1169         Label loop;
1170         __ bind(loop);
1171         __ movptr(rbx_temp, Address(rsi_source, 0));
1172         __ movptr(Address(rax_argslot, 0), rbx_temp);
1173         __ addptr(rsi_source, type2aelembytes(elem_type));
1174         __ addptr(rax_argslot, Interpreter::stackElementSize);
1175         __ cmpptr(rax_argslot, rdx_argslot_limit);
1176         __ jccb(Assembler::less, loop);
1177       } else if (length_constant == 0) {
1178         __ bind(skip_array_check);
1179         // nothing to copy
1180       } else {
1181         int elem_offset = elem0_offset;
1182         int slot_offset = 0;
1183         for (int index = 0; index < length_constant; index++) {
1184           __ movptr(rbx_temp, Address(rsi_array, elem_offset));
1185           __ movptr(Address(rax_argslot, slot_offset), rbx_temp);
1186           elem_offset += type2aelembytes(elem_type);
1187            slot_offset += Interpreter::stackElementSize;
1188         }
1189       }
1190 
1191       // Arguments are spread.  Move to next method handle.
1192       UNPUSH_RSI_RDI;
1193       __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1194       __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1195 
1196       __ bind(bad_array_klass);
1197       UNPUSH_RSI_RDI;
1198       assert(!vmarg.uses(rarg2_required), "must be different registers");
1199       __ movptr(rarg2_required, Address(rdx_array_klass, java_mirror_offset));  // required type
1200       __ movptr(rarg1_actual,   vmarg);                                         // bad array
1201       __ movl(  rarg0_code,     (int) Bytecodes::_aaload);                      // who is complaining?
1202       __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
1203 
1204       __ bind(bad_array_length);
1205       UNPUSH_RSI_RDI;
1206       assert(!vmarg.uses(rarg2_required), "must be different registers");
1207       __ mov   (rarg2_required, rcx_recv);                       // AMH requiring a certain length
1208       __ movptr(rarg1_actual,   vmarg);                          // bad array
1209       __ movl(  rarg0_code,     (int) Bytecodes::_arraylength);  // who is complaining?
1210       __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
1211 
1212 #undef UNPUSH_RSI_RDI
1213     }
1214     break;
1215 
1216   case _adapter_flyby:
1217   case _adapter_ricochet:
1218     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
1219     break;
1220 
1221   default:  ShouldNotReachHere();
1222   }
1223   __ hlt();
1224 
1225   address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
1226   __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
1227 
1228   init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
1229 }
--- EOF ---