1 /*
   2  * Copyright (c) 2008, 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 address MethodHandleEntry::start_compiled_entry(MacroAssembler* _masm,
  33                                                 address interpreted_entry) {
  34   // Just before the actual machine code entry point, allocate space
  35   // for a MethodHandleEntry::Data record, so that we can manage everything
  36   // from one base pointer.
  37   __ align(wordSize);
  38   address target = __ pc() + sizeof(Data);
  39   while (__ pc() < target) {
  40     __ nop();
  41     __ align(wordSize);
  42   }
  43 
  44   MethodHandleEntry* me = (MethodHandleEntry*) __ pc();
  45   me->set_end_address(__ pc());         // set a temporary end_address
  46   me->set_from_interpreted_entry(interpreted_entry);
  47   me->set_type_checking_entry(NULL);
  48 
  49   return (address) me;
  50 }
  51 
  52 MethodHandleEntry* MethodHandleEntry::finish_compiled_entry(MacroAssembler* _masm,
  53                                                 address start_addr) {
  54   MethodHandleEntry* me = (MethodHandleEntry*) start_addr;
  55   assert(me->end_address() == start_addr, "valid ME");
  56 
  57   // Fill in the real end_address:
  58   __ align(wordSize);
  59   me->set_end_address(__ pc());
  60 
  61   return me;
  62 }
  63 
  64 
  65 // Code generation
  66 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
  67   // I5_savedSP: sender SP (must preserve)
  68   // G4 (Gargs): incoming argument list (must preserve)
  69   // G5_method:  invoke methodOop; becomes method type.
  70   // G3_method_handle: receiver method handle (must load from sp[MethodTypeForm.vmslots])
  71   // O0, O1: garbage temps, blown away
  72   Register O0_argslot = O0;
  73   Register O1_scratch = O1;
  74 
  75   // emit WrongMethodType path first, to enable back-branch from main path
  76   Label wrong_method_type;
  77   __ bind(wrong_method_type);
  78   __ jump_to(AddressLiteral(Interpreter::throw_WrongMethodType_entry()), O1_scratch);
  79   __ delayed()->nop();
  80 
  81   // here's where control starts out:
  82   __ align(CodeEntryAlignment);
  83   address entry_point = __ pc();
  84 
  85   // fetch the MethodType from the method handle into G5_method_type
  86   {
  87     Register tem = G5_method;
  88     assert(tem == G5_method_type, "yes, it's the same register");
  89     for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
  90       __ ld_ptr(Address(tem, *pchase), G5_method_type);
  91     }
  92   }
  93 
  94   // given the MethodType, find out where the MH argument is buried
  95   __ ld_ptr(Address(G5_method_type, __ delayed_value(java_dyn_MethodType::form_offset_in_bytes, O1_scratch)),        O0_argslot);
  96   __ ldsw(  Address(O0_argslot,     __ delayed_value(java_dyn_MethodTypeForm::vmslots_offset_in_bytes, O1_scratch)), O0_argslot);
  97   __ ld_ptr(__ argument_address(O0_argslot), G3_method_handle);
  98 
  99   __ check_method_handle_type(G5_method_type, G3_method_handle, O1_scratch, wrong_method_type);
 100   __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 101 
 102   return entry_point;
 103 }
 104 
 105 
 106 #ifdef ASSERT
 107 static void verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
 108   // Verify that argslot lies within (Gargs, FP].
 109   Label L_ok, L_bad;
 110 #ifdef _LP64
 111   __ add(FP, STACK_BIAS, temp_reg);
 112   __ cmp(argslot_reg, temp_reg);
 113 #else
 114   __ cmp(argslot_reg, FP);
 115 #endif
 116   __ brx(Assembler::greaterUnsigned, false, Assembler::pn, L_bad);
 117   __ delayed()->nop();
 118   __ cmp(Gargs, argslot_reg);
 119   __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
 120   __ delayed()->nop();
 121   __ bind(L_bad);
 122   __ stop(error_message);
 123   __ bind(L_ok);
 124 }
 125 #endif
 126 
 127 
 128 // Helper to insert argument slots into the stack.
 129 // arg_slots must be a multiple of stack_move_unit() and <= 0
 130 void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
 131                                      RegisterOrConstant arg_slots,
 132                                      int arg_mask,
 133                                      Register argslot_reg,
 134                                      Register temp_reg, Register temp2_reg, Register temp3_reg) {
 135   assert(temp3_reg != noreg, "temp3 required");
 136   assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
 137                              (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
 138 
 139 #ifdef ASSERT
 140   verify_argslot(_masm, argslot_reg, temp_reg, "insertion point must fall within current frame");
 141   if (arg_slots.is_register()) {
 142     Label L_ok, L_bad;
 143     __ cmp(arg_slots.as_register(), (int32_t) NULL_WORD);
 144     __ br(Assembler::greater, false, Assembler::pn, L_bad);
 145     __ delayed()->nop();
 146     __ btst(-stack_move_unit() - 1, arg_slots.as_register());
 147     __ br(Assembler::zero, false, Assembler::pt, L_ok);
 148     __ delayed()->nop();
 149     __ bind(L_bad);
 150     __ stop("assert arg_slots <= 0 and clear low bits");
 151     __ bind(L_ok);
 152   } else {
 153     assert(arg_slots.as_constant() <= 0, "");
 154     assert(arg_slots.as_constant() % -stack_move_unit() == 0, "");
 155   }
 156 #endif // ASSERT
 157 
 158 #ifdef _LP64
 159   if (arg_slots.is_register()) {
 160     // Was arg_slots register loaded as signed int?
 161     Label L_ok;
 162     __ sll(arg_slots.as_register(), BitsPerInt, temp_reg);
 163     __ sra(temp_reg, BitsPerInt, temp_reg);
 164     __ cmp(arg_slots.as_register(), temp_reg);
 165     __ br(Assembler::equal, false, Assembler::pt, L_ok);
 166     __ delayed()->nop();
 167     __ stop("arg_slots register not loaded as signed int");
 168     __ bind(L_ok);
 169   }
 170 #endif
 171 
 172   // Make space on the stack for the inserted argument(s).
 173   // Then pull down everything shallower than argslot_reg.
 174   // The stacked return address gets pulled down with everything else.
 175   // That is, copy [sp, argslot) downward by -size words.  In pseudo-code:
 176   //   sp -= size;
 177   //   for (temp = sp + size; temp < argslot; temp++)
 178   //     temp[-size] = temp[0]
 179   //   argslot -= size;
 180   RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
 181 
 182   // Keep the stack pointer 2*wordSize aligned.
 183   const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
 184   RegisterOrConstant masked_offset = __ regcon_andn_ptr(offset, TwoWordAlignmentMask, temp_reg);
 185   __ add(SP, masked_offset, SP);
 186 
 187   __ mov(Gargs, temp_reg);  // source pointer for copy
 188   __ add(Gargs, offset, Gargs);
 189 
 190   {
 191     Label loop;
 192     __ bind(loop);
 193     // pull one word down each time through the loop
 194     __ ld_ptr(Address(temp_reg, 0), temp2_reg);
 195     __ st_ptr(temp2_reg, Address(temp_reg, offset));
 196     __ add(temp_reg, wordSize, temp_reg);
 197     __ cmp(temp_reg, argslot_reg);
 198     __ brx(Assembler::less, false, Assembler::pt, loop);
 199     __ delayed()->nop();  // FILLME
 200   }
 201 
 202   // Now move the argslot down, to point to the opened-up space.
 203   __ add(argslot_reg, offset, argslot_reg);
 204 }
 205 
 206 
 207 // Helper to remove argument slots from the stack.
 208 // arg_slots must be a multiple of stack_move_unit() and >= 0
 209 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
 210                                      RegisterOrConstant arg_slots,
 211                                      Register argslot_reg,
 212                                      Register temp_reg, Register temp2_reg, Register temp3_reg) {
 213   assert(temp3_reg != noreg, "temp3 required");
 214   assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
 215                              (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
 216 
 217   RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
 218 
 219 #ifdef ASSERT
 220   // Verify that [argslot..argslot+size) lies within (Gargs, FP).
 221   __ add(argslot_reg, offset, temp2_reg);
 222   verify_argslot(_masm, temp2_reg, temp_reg, "deleted argument(s) must fall within current frame");
 223   if (arg_slots.is_register()) {
 224     Label L_ok, L_bad;
 225     __ cmp(arg_slots.as_register(), (int32_t) NULL_WORD);
 226     __ br(Assembler::less, false, Assembler::pn, L_bad);
 227     __ delayed()->nop();
 228     __ btst(-stack_move_unit() - 1, arg_slots.as_register());
 229     __ br(Assembler::zero, false, Assembler::pt, L_ok);
 230     __ delayed()->nop();
 231     __ bind(L_bad);
 232     __ stop("assert arg_slots >= 0 and clear low bits");
 233     __ bind(L_ok);
 234   } else {
 235     assert(arg_slots.as_constant() >= 0, "");
 236     assert(arg_slots.as_constant() % -stack_move_unit() == 0, "");
 237   }
 238 #endif // ASSERT
 239 
 240   // Pull up everything shallower than argslot.
 241   // Then remove the excess space on the stack.
 242   // The stacked return address gets pulled up with everything else.
 243   // That is, copy [sp, argslot) upward by size words.  In pseudo-code:
 244   //   for (temp = argslot-1; temp >= sp; --temp)
 245   //     temp[size] = temp[0]
 246   //   argslot += size;
 247   //   sp += size;
 248   __ sub(argslot_reg, wordSize, temp_reg);  // source pointer for copy
 249   {
 250     Label loop;
 251     __ bind(loop);
 252     // pull one word up each time through the loop
 253     __ ld_ptr(Address(temp_reg, 0), temp2_reg);
 254     __ st_ptr(temp2_reg, Address(temp_reg, offset));
 255     __ sub(temp_reg, wordSize, temp_reg);
 256     __ cmp(temp_reg, Gargs);
 257     __ brx(Assembler::greaterEqual, false, Assembler::pt, loop);
 258     __ delayed()->nop();  // FILLME
 259   }
 260 
 261   // Now move the argslot up, to point to the just-copied block.
 262   __ add(Gargs, offset, Gargs);
 263   // And adjust the argslot address to point at the deletion point.
 264   __ add(argslot_reg, offset, argslot_reg);
 265 
 266   // Keep the stack pointer 2*wordSize aligned.
 267   const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
 268   RegisterOrConstant masked_offset = __ regcon_andn_ptr(offset, TwoWordAlignmentMask, temp_reg);
 269   __ add(SP, masked_offset, SP);
 270 }
 271 
 272 
 273 #ifndef PRODUCT
 274 extern "C" void print_method_handle(oop mh);
 275 void trace_method_handle_stub(const char* adaptername,
 276                               oop mh) {
 277 #if 0
 278                               intptr_t* entry_sp,
 279                               intptr_t* saved_sp,
 280                               intptr_t* saved_bp) {
 281   // called as a leaf from native code: do not block the JVM!
 282   intptr_t* last_sp = (intptr_t*) saved_bp[frame::interpreter_frame_last_sp_offset];
 283   intptr_t* base_sp = (intptr_t*) saved_bp[frame::interpreter_frame_monitor_block_top_offset];
 284   printf("MH %s mh="INTPTR_FORMAT" sp=("INTPTR_FORMAT"+"INTX_FORMAT") stack_size="INTX_FORMAT" bp="INTPTR_FORMAT"\n",
 285          adaptername, (intptr_t)mh, (intptr_t)entry_sp, (intptr_t)(saved_sp - entry_sp), (intptr_t)(base_sp - last_sp), (intptr_t)saved_bp);
 286   if (last_sp != saved_sp)
 287     printf("*** last_sp="INTPTR_FORMAT"\n", (intptr_t)last_sp);
 288 #endif
 289 
 290   printf("MH %s mh="INTPTR_FORMAT"\n", adaptername, (intptr_t) mh);
 291   print_method_handle(mh);
 292 }
 293 #endif // PRODUCT
 294 
 295 // which conversion op types are implemented here?
 296 int MethodHandles::adapter_conversion_ops_supported_mask() {
 297   return ((1<<sun_dyn_AdapterMethodHandle::OP_RETYPE_ONLY)
 298          |(1<<sun_dyn_AdapterMethodHandle::OP_RETYPE_RAW)
 299          |(1<<sun_dyn_AdapterMethodHandle::OP_CHECK_CAST)
 300          |(1<<sun_dyn_AdapterMethodHandle::OP_PRIM_TO_PRIM)
 301          |(1<<sun_dyn_AdapterMethodHandle::OP_REF_TO_PRIM)
 302          |(1<<sun_dyn_AdapterMethodHandle::OP_SWAP_ARGS)
 303          |(1<<sun_dyn_AdapterMethodHandle::OP_ROT_ARGS)
 304          |(1<<sun_dyn_AdapterMethodHandle::OP_DUP_ARGS)
 305          |(1<<sun_dyn_AdapterMethodHandle::OP_DROP_ARGS)
 306          //|(1<<sun_dyn_AdapterMethodHandle::OP_SPREAD_ARGS) //BUG!
 307          );
 308   // FIXME: MethodHandlesTest gets a crash if we enable OP_SPREAD_ARGS.
 309 }
 310 
 311 //------------------------------------------------------------------------------
 312 // MethodHandles::generate_method_handle_stub
 313 //
 314 // Generate an "entry" field for a method handle.
 315 // This determines how the method handle will respond to calls.
 316 void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek) {
 317   // Here is the register state during an interpreted call,
 318   // as set up by generate_method_handle_interpreter_entry():
 319   // - G5: garbage temp (was MethodHandle.invoke methodOop, unused)
 320   // - G3: receiver method handle
 321   // - O5_savedSP: sender SP (must preserve)
 322 
 323   Register O0_argslot = O0;
 324   Register O1_scratch = O1;
 325   Register O2_scratch = O2;
 326   Register O3_scratch = O3;
 327   Register G5_index   = G5;
 328 
 329   guarantee(java_dyn_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
 330 
 331   // Some handy addresses:
 332   Address G5_method_fie(    G5_method,        in_bytes(methodOopDesc::from_interpreted_offset()));
 333 
 334   Address G3_mh_vmtarget(   G3_method_handle, java_dyn_MethodHandle::vmtarget_offset_in_bytes());
 335 
 336   Address G3_dmh_vmindex(   G3_method_handle, sun_dyn_DirectMethodHandle::vmindex_offset_in_bytes());
 337 
 338   Address G3_bmh_vmargslot( G3_method_handle, sun_dyn_BoundMethodHandle::vmargslot_offset_in_bytes());
 339   Address G3_bmh_argument(  G3_method_handle, sun_dyn_BoundMethodHandle::argument_offset_in_bytes());
 340 
 341   Address G3_amh_vmargslot( G3_method_handle, sun_dyn_AdapterMethodHandle::vmargslot_offset_in_bytes());
 342   Address G3_amh_argument ( G3_method_handle, sun_dyn_AdapterMethodHandle::argument_offset_in_bytes());
 343   Address G3_amh_conversion(G3_method_handle, sun_dyn_AdapterMethodHandle::conversion_offset_in_bytes());
 344 
 345   const int java_mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 346 
 347   if (have_entry(ek)) {
 348     __ nop();  // empty stubs make SG sick
 349     return;
 350   }
 351 
 352   address interp_entry = __ pc();
 353   if (UseCompressedOops)  __ unimplemented("UseCompressedOops");
 354 
 355 #ifndef PRODUCT
 356   if (TraceMethodHandles) {
 357     // save: Gargs, O5_savedSP
 358     __ save(SP, -16*wordSize, SP);
 359     __ set((intptr_t) entry_name(ek), O0);
 360     __ mov(G3_method_handle, O1);
 361     __ call_VM_leaf(Lscratch, CAST_FROM_FN_PTR(address, trace_method_handle_stub));
 362     __ restore(SP, 16*wordSize, SP);
 363   }
 364 #endif // PRODUCT
 365 
 366   switch ((int) ek) {
 367   case _raise_exception:
 368     {
 369       // Not a real MH entry, but rather shared code for raising an
 370       // exception.  Extra local arguments are passed in scratch
 371       // registers, as required type in O3, failing object (or NULL)
 372       // in O2, failing bytecode type in O1.
 373 
 374       __ mov(O5_savedSP, SP);  // Cut the stack back to where the caller started.
 375 
 376       // Push arguments as if coming from the interpreter.
 377       Register O0_scratch = O0_argslot;
 378       int stackElementSize = Interpreter::stackElementSize;
 379 
 380       // Make space on the stack for the arguments and set Gargs
 381       // correctly.
 382       __ sub(SP, 4*stackElementSize, SP);  // Keep stack aligned.
 383       __ add(SP, (frame::varargs_offset)*wordSize - 1*Interpreter::stackElementSize + STACK_BIAS + BytesPerWord, Gargs);
 384 
 385       // void raiseException(int code, Object actual, Object required)
 386       __ st(    O1_scratch, Address(Gargs, 2*stackElementSize));  // code
 387       __ st_ptr(O2_scratch, Address(Gargs, 1*stackElementSize));  // actual
 388       __ st_ptr(O3_scratch, Address(Gargs, 0*stackElementSize));  // required
 389 
 390       Label no_method;
 391       // FIXME: fill in _raise_exception_method with a suitable sun.dyn method
 392       __ set(AddressLiteral((address) &_raise_exception_method), G5_method);
 393       __ ld_ptr(Address(G5_method, 0), G5_method);
 394       __ tst(G5_method);
 395       __ brx(Assembler::zero, false, Assembler::pn, no_method);
 396       __ delayed()->nop();
 397 
 398       int jobject_oop_offset = 0;
 399       __ ld_ptr(Address(G5_method, jobject_oop_offset), G5_method);
 400       __ tst(G5_method);
 401       __ brx(Assembler::zero, false, Assembler::pn, no_method);
 402       __ delayed()->nop();
 403 
 404       __ verify_oop(G5_method);
 405       __ jump_indirect_to(G5_method_fie, O1_scratch);
 406       __ delayed()->nop();
 407 
 408       // If we get here, the Java runtime did not do its job of creating the exception.
 409       // Do something that is at least causes a valid throw from the interpreter.
 410       __ bind(no_method);
 411       __ unimplemented("_raise_exception no method");
 412     }
 413     break;
 414 
 415   case _invokestatic_mh:
 416   case _invokespecial_mh:
 417     {
 418       __ ld_ptr(G3_mh_vmtarget, G5_method);  // target is a methodOop
 419       __ verify_oop(G5_method);
 420       // Same as TemplateTable::invokestatic or invokespecial,
 421       // minus the CP setup and profiling:
 422       if (ek == _invokespecial_mh) {
 423         // Must load & check the first argument before entering the target method.
 424         __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
 425         __ ld_ptr(__ argument_address(O0_argslot), G3_method_handle);
 426         __ null_check(G3_method_handle);
 427         __ verify_oop(G3_method_handle);
 428       }
 429       __ jump_indirect_to(G5_method_fie, O1_scratch);
 430       __ delayed()->nop();
 431     }
 432     break;
 433 
 434   case _invokevirtual_mh:
 435     {
 436       // Same as TemplateTable::invokevirtual,
 437       // minus the CP setup and profiling:
 438 
 439       // Pick out the vtable index and receiver offset from the MH,
 440       // and then we can discard it:
 441       __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
 442       __ ldsw(G3_dmh_vmindex, G5_index);
 443       // Note:  The verifier allows us to ignore G3_mh_vmtarget.
 444       __ ld_ptr(__ argument_address(O0_argslot, -1), G3_method_handle);
 445       __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
 446 
 447       // Get receiver klass:
 448       Register O0_klass = O0_argslot;
 449       __ load_klass(G3_method_handle, O0_klass);
 450       __ verify_oop(O0_klass);
 451 
 452       // Get target methodOop & entry point:
 453       const int base = instanceKlass::vtable_start_offset() * wordSize;
 454       assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
 455 
 456       __ sll_ptr(G5_index, LogBytesPerWord, G5_index);
 457       __ add(O0_klass, G5_index, O0_klass);
 458       Address vtable_entry_addr(O0_klass, base + vtableEntry::method_offset_in_bytes());
 459       __ ld_ptr(vtable_entry_addr, G5_method);
 460 
 461       __ verify_oop(G5_method);
 462       __ jump_indirect_to(G5_method_fie, O1_scratch);
 463       __ delayed()->nop();
 464     }
 465     break;
 466 
 467   case _invokeinterface_mh:
 468     {
 469       // Same as TemplateTable::invokeinterface,
 470       // minus the CP setup and profiling:
 471       __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
 472       Register O1_intf  = O1_scratch;
 473       __ ld_ptr(G3_mh_vmtarget, O1_intf);
 474       __ ldsw(G3_dmh_vmindex, G5_index);
 475       __ ld_ptr(__ argument_address(O0_argslot, -1), G3_method_handle);
 476       __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
 477 
 478       // Get receiver klass:
 479       Register O0_klass = O0_argslot;
 480       __ load_klass(G3_method_handle, O0_klass);
 481       __ verify_oop(O0_klass);
 482 
 483       // Get interface:
 484       Label no_such_interface;
 485       __ verify_oop(O1_intf);
 486       __ lookup_interface_method(O0_klass, O1_intf,
 487                                  // Note: next two args must be the same:
 488                                  G5_index, G5_method,
 489                                  O2_scratch,
 490                                  O3_scratch,
 491                                  no_such_interface);
 492 
 493       __ verify_oop(G5_method);
 494       __ jump_indirect_to(G5_method_fie, O1_scratch);
 495       __ delayed()->nop();
 496 
 497       __ bind(no_such_interface);
 498       // Throw an exception.
 499       // For historical reasons, it will be IncompatibleClassChangeError.
 500       __ unimplemented("not tested yet");
 501       __ ld_ptr(Address(O1_intf, java_mirror_offset), O3_scratch);  // required interface
 502       __ mov(O0_klass, O2_scratch);  // bad receiver
 503       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O0_argslot);
 504       __ delayed()->mov(Bytecodes::_invokeinterface, O1_scratch);  // who is complaining?
 505     }
 506     break;
 507 
 508   case _bound_ref_mh:
 509   case _bound_int_mh:
 510   case _bound_long_mh:
 511   case _bound_ref_direct_mh:
 512   case _bound_int_direct_mh:
 513   case _bound_long_direct_mh:
 514     {
 515       const bool direct_to_method = (ek >= _bound_ref_direct_mh);
 516       BasicType arg_type  = T_ILLEGAL;
 517       int       arg_mask  = _INSERT_NO_MASK;
 518       int       arg_slots = -1;
 519       get_ek_bound_mh_info(ek, arg_type, arg_mask, arg_slots);
 520 
 521       // Make room for the new argument:
 522       __ ldsw(G3_bmh_vmargslot, O0_argslot);
 523       __ add(Gargs, __ argument_offset(O0_argslot), O0_argslot);
 524 
 525       insert_arg_slots(_masm, arg_slots * stack_move_unit(), arg_mask, O0_argslot, O1_scratch, O2_scratch, G5_index);
 526 
 527       // Store bound argument into the new stack slot:
 528       __ ld_ptr(G3_bmh_argument, O1_scratch);
 529       if (arg_type == T_OBJECT) {
 530         __ st_ptr(O1_scratch, Address(O0_argslot, 0));
 531       } else {
 532         Address prim_value_addr(O1_scratch, java_lang_boxing_object::value_offset_in_bytes(arg_type));
 533         __ load_sized_value(prim_value_addr, O2_scratch, type2aelembytes(arg_type), is_signed_subword_type(arg_type));
 534         if (arg_slots == 2) {
 535           __ unimplemented("not yet tested");
 536 #ifndef _LP64
 537           __ signx(O2_scratch, O3_scratch);  // Sign extend
 538 #endif
 539           __ st_long(O2_scratch, Address(O0_argslot, 0));  // Uses O2/O3 on !_LP64
 540         } else {
 541           __ st_ptr( O2_scratch, Address(O0_argslot, 0));
 542         }
 543       }
 544 
 545       if (direct_to_method) {
 546         __ ld_ptr(G3_mh_vmtarget, G5_method);  // target is a methodOop
 547         __ verify_oop(G5_method);
 548         __ jump_indirect_to(G5_method_fie, O1_scratch);
 549         __ delayed()->nop();
 550       } else {
 551         __ ld_ptr(G3_mh_vmtarget, G3_method_handle);  // target is a methodOop
 552         __ verify_oop(G3_method_handle);
 553         __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 554       }
 555     }
 556     break;
 557 
 558   case _adapter_retype_only:
 559   case _adapter_retype_raw:
 560     // Immediately jump to the next MH layer:
 561     __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 562     __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 563     // This is OK when all parameter types widen.
 564     // It is also OK when a return type narrows.
 565     break;
 566 
 567   case _adapter_check_cast:
 568     {
 569       // Temps:
 570       Register G5_klass = G5_index;  // Interesting AMH data.
 571 
 572       // Check a reference argument before jumping to the next layer of MH:
 573       __ ldsw(G3_amh_vmargslot, O0_argslot);
 574       Address vmarg = __ argument_address(O0_argslot);
 575 
 576       // What class are we casting to?
 577       __ ld_ptr(G3_amh_argument, G5_klass);  // This is a Class object!
 578       __ ld_ptr(Address(G5_klass, java_lang_Class::klass_offset_in_bytes()), G5_klass);
 579 
 580       Label done;
 581       __ ld_ptr(vmarg, O1_scratch);
 582       __ tst(O1_scratch);
 583       __ brx(Assembler::zero, false, Assembler::pn, done);  // No cast if null.
 584       __ delayed()->nop();
 585       __ load_klass(O1_scratch, O1_scratch);
 586 
 587       // Live at this point:
 588       // - G5_klass        :  klass required by the target method
 589       // - O1_scratch      :  argument klass to test
 590       // - G3_method_handle:  adapter method handle
 591       __ check_klass_subtype(O1_scratch, G5_klass, O0_argslot, O2_scratch, done);
 592 
 593       // If we get here, the type check failed!
 594       __ ldsw(G3_amh_vmargslot, O0_argslot);  // reload argslot field
 595       __ ld_ptr(G3_amh_argument, O3_scratch);  // required class
 596       __ ld_ptr(vmarg, O2_scratch);  // bad object
 597       __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O0_argslot);
 598       __ delayed()->mov(Bytecodes::_checkcast, O1_scratch);  // who is complaining?
 599 
 600       __ bind(done);
 601       // Get the new MH:
 602       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 603       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 604     }
 605     break;
 606 
 607   case _adapter_prim_to_prim:
 608   case _adapter_ref_to_prim:
 609     // Handled completely by optimized cases.
 610     __ stop("init_AdapterMethodHandle should not issue this");
 611     break;
 612 
 613   case _adapter_opt_i2i:        // optimized subcase of adapt_prim_to_prim
 614 //case _adapter_opt_f2i:        // optimized subcase of adapt_prim_to_prim
 615   case _adapter_opt_l2i:        // optimized subcase of adapt_prim_to_prim
 616   case _adapter_opt_unboxi:     // optimized subcase of adapt_ref_to_prim
 617     {
 618       // Perform an in-place conversion to int or an int subword.
 619       __ ldsw(G3_amh_vmargslot, O0_argslot);
 620       Address vmarg = __ argument_address(O0_argslot);
 621       Address value;
 622       bool value_left_justified = false;
 623 
 624       switch (ek) {
 625       case _adapter_opt_i2i:
 626       case _adapter_opt_l2i:
 627         __ unimplemented(entry_name(ek));
 628         value = vmarg;
 629         break;
 630       case _adapter_opt_unboxi:
 631         {
 632           // Load the value up from the heap.
 633           __ ld_ptr(vmarg, O1_scratch);
 634           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
 635 #ifdef ASSERT
 636           for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
 637             if (is_subword_type(BasicType(bt)))
 638               assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
 639           }
 640 #endif
 641           __ null_check(O1_scratch, value_offset);
 642           value = Address(O1_scratch, value_offset);
 643 #ifdef _BIG_ENDIAN
 644           // Values stored in objects are packed.
 645           value_left_justified = true;
 646 #endif
 647         }
 648         break;
 649       default:
 650         ShouldNotReachHere();
 651       }
 652 
 653       // This check is required on _BIG_ENDIAN
 654       Register G5_vminfo = G5_index;
 655       __ ldsw(G3_amh_conversion, G5_vminfo);
 656       assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 657 
 658       // Original 32-bit vmdata word must be of this form:
 659       // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
 660       __ lduw(value, O1_scratch);
 661       if (!value_left_justified)
 662         __ sll(O1_scratch, G5_vminfo, O1_scratch);
 663       Label zero_extend, done;
 664       __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
 665       __ br(Assembler::zero, false, Assembler::pn, zero_extend);
 666       __ delayed()->nop();
 667 
 668       // this path is taken for int->byte, int->short
 669       __ sra(O1_scratch, G5_vminfo, O1_scratch);
 670       __ ba(false, done);
 671       __ delayed()->nop();
 672 
 673       __ bind(zero_extend);
 674       // this is taken for int->char
 675       __ srl(O1_scratch, G5_vminfo, O1_scratch);
 676 
 677       __ bind(done);
 678       __ st(O1_scratch, vmarg);
 679 
 680       // Get the new MH:
 681       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 682       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 683     }
 684     break;
 685 
 686   case _adapter_opt_i2l:        // optimized subcase of adapt_prim_to_prim
 687   case _adapter_opt_unboxl:     // optimized subcase of adapt_ref_to_prim
 688     {
 689       // Perform an in-place int-to-long or ref-to-long conversion.
 690       __ ldsw(G3_amh_vmargslot, O0_argslot);
 691 
 692       // On big-endian machine we duplicate the slot and store the MSW
 693       // in the first slot.
 694       __ add(Gargs, __ argument_offset(O0_argslot, 1), O0_argslot);
 695 
 696       insert_arg_slots(_masm, stack_move_unit(), _INSERT_INT_MASK, O0_argslot, O1_scratch, O2_scratch, G5_index);
 697 
 698       Address arg_lsw(O0_argslot, 0);
 699       Address arg_msw(O0_argslot, -Interpreter::stackElementSize);
 700 
 701       switch (ek) {
 702       case _adapter_opt_i2l:
 703         {
 704           __ ldsw(arg_lsw, O2_scratch);      // Load LSW
 705 #ifndef _LP64
 706           __ signx(O2_scratch, O3_scratch);  // Sign extend
 707 #endif
 708           __ st_long(O2_scratch, arg_msw);   // Uses O2/O3 on !_LP64
 709         }
 710         break;
 711       case _adapter_opt_unboxl:
 712         {
 713           // Load the value up from the heap.
 714           __ ld_ptr(arg_lsw, O1_scratch);
 715           int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
 716           assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
 717           __ null_check(O1_scratch, value_offset);
 718           __ ld_long(Address(O1_scratch, value_offset), O2_scratch);  // Uses O2/O3 on !_LP64
 719           __ st_long(O2_scratch, arg_msw);
 720         }
 721         break;
 722       default:
 723         ShouldNotReachHere();
 724       }
 725 
 726       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 727       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 728     }
 729     break;
 730 
 731   case _adapter_opt_f2d:        // optimized subcase of adapt_prim_to_prim
 732   case _adapter_opt_d2f:        // optimized subcase of adapt_prim_to_prim
 733     {
 734       // perform an in-place floating primitive conversion
 735       __ unimplemented(entry_name(ek));
 736     }
 737     break;
 738 
 739   case _adapter_prim_to_ref:
 740     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
 741     break;
 742 
 743   case _adapter_swap_args:
 744   case _adapter_rot_args:
 745     // handled completely by optimized cases
 746     __ stop("init_AdapterMethodHandle should not issue this");
 747     break;
 748 
 749   case _adapter_opt_swap_1:
 750   case _adapter_opt_swap_2:
 751   case _adapter_opt_rot_1_up:
 752   case _adapter_opt_rot_1_down:
 753   case _adapter_opt_rot_2_up:
 754   case _adapter_opt_rot_2_down:
 755     {
 756       int swap_bytes = 0, rotate = 0;
 757       get_ek_adapter_opt_swap_rot_info(ek, swap_bytes, rotate);
 758 
 759       // 'argslot' is the position of the first argument to swap.
 760       __ ldsw(G3_amh_vmargslot, O0_argslot);
 761       __ add(Gargs, __ argument_offset(O0_argslot), O0_argslot);
 762 
 763       // 'vminfo' is the second.
 764       Register O1_destslot = O1_scratch;
 765       __ ldsw(G3_amh_conversion, O1_destslot);
 766       assert(CONV_VMINFO_SHIFT == 0, "preshifted");
 767       __ and3(O1_destslot, CONV_VMINFO_MASK, O1_destslot);
 768       __ add(Gargs, __ argument_offset(O1_destslot), O1_destslot);
 769 
 770       if (!rotate) {
 771         for (int i = 0; i < swap_bytes; i += wordSize) {
 772           __ ld_ptr(Address(O0_argslot,  i), O2_scratch);
 773           __ ld_ptr(Address(O1_destslot, i), O3_scratch);
 774           __ st_ptr(O3_scratch, Address(O0_argslot,  i));
 775           __ st_ptr(O2_scratch, Address(O1_destslot, i));
 776         }
 777       } else {
 778         // Save the first chunk, which is going to get overwritten.
 779         switch (swap_bytes) {
 780         case 4 : __ lduw(Address(O0_argslot, 0), O2_scratch); break;
 781         case 16: __ ldx( Address(O0_argslot, 8), O3_scratch); //fall-thru
 782         case 8 : __ ldx( Address(O0_argslot, 0), O2_scratch); break;
 783         default: ShouldNotReachHere();
 784         }
 785 
 786         if (rotate > 0) {
 787           // Rorate upward.
 788           __ sub(O0_argslot, swap_bytes, O0_argslot);
 789 #if ASSERT
 790           {
 791             // Verify that argslot > destslot, by at least swap_bytes.
 792             Label L_ok;
 793             __ cmp(O0_argslot, O1_destslot);
 794             __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, L_ok);
 795             __ delayed()->nop();
 796             __ stop("source must be above destination (upward rotation)");
 797             __ bind(L_ok);
 798           }
 799 #endif
 800           // Work argslot down to destslot, copying contiguous data upwards.
 801           // Pseudo-code:
 802           //   argslot  = src_addr - swap_bytes
 803           //   destslot = dest_addr
 804           //   while (argslot >= destslot) {
 805           //     *(argslot + swap_bytes) = *(argslot + 0);
 806           //     argslot--;
 807           //   }
 808           Label loop;
 809           __ bind(loop);
 810           __ ld_ptr(Address(O0_argslot, 0), G5_index);
 811           __ st_ptr(G5_index, Address(O0_argslot, swap_bytes));
 812           __ sub(O0_argslot, wordSize, O0_argslot);
 813           __ cmp(O0_argslot, O1_destslot);
 814           __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, loop);
 815           __ delayed()->nop();  // FILLME
 816         } else {
 817           __ add(O0_argslot, swap_bytes, O0_argslot);
 818 #if ASSERT
 819           {
 820             // Verify that argslot < destslot, by at least swap_bytes.
 821             Label L_ok;
 822             __ cmp(O0_argslot, O1_destslot);
 823             __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
 824             __ delayed()->nop();
 825             __ stop("source must be above destination (upward rotation)");
 826             __ bind(L_ok);
 827           }
 828 #endif
 829           // Work argslot up to destslot, copying contiguous data downwards.
 830           // Pseudo-code:
 831           //   argslot  = src_addr + swap_bytes
 832           //   destslot = dest_addr
 833           //   while (argslot >= destslot) {
 834           //     *(argslot - swap_bytes) = *(argslot + 0);
 835           //     argslot++;
 836           //   }
 837           Label loop;
 838           __ bind(loop);
 839           __ ld_ptr(Address(O0_argslot, 0), G5_index);
 840           __ st_ptr(G5_index, Address(O0_argslot, -swap_bytes));
 841           __ add(O0_argslot, wordSize, O0_argslot);
 842           __ cmp(O0_argslot, O1_destslot);
 843           __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, loop);
 844           __ delayed()->nop();  // FILLME
 845         }
 846 
 847         // Store the original first chunk into the destination slot, now free.
 848         switch (swap_bytes) {
 849         case 4 : __ stw(O2_scratch, Address(O1_destslot, 0)); break;
 850         case 16: __ stx(O3_scratch, Address(O1_destslot, 8)); // fall-thru
 851         case 8 : __ stx(O2_scratch, Address(O1_destslot, 0)); break;
 852         default: ShouldNotReachHere();
 853         }
 854       }
 855 
 856       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 857       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 858     }
 859     break;
 860 
 861   case _adapter_dup_args:
 862     {
 863       // 'argslot' is the position of the first argument to duplicate.
 864       __ ldsw(G3_amh_vmargslot, O0_argslot);
 865       __ add(Gargs, __ argument_offset(O0_argslot), O0_argslot);
 866 
 867       // 'stack_move' is negative number of words to duplicate.
 868       Register G5_stack_move = G5_index;
 869       __ ldsw(G3_amh_conversion, G5_stack_move);
 870       __ sra(G5_stack_move, CONV_STACK_MOVE_SHIFT, G5_stack_move);
 871 
 872       // Remember the old Gargs (argslot[0]).
 873       Register O1_oldarg = O1_scratch;
 874       __ mov(Gargs, O1_oldarg);
 875 
 876       // Move Gargs down to make room for dups.
 877       __ sll_ptr(G5_stack_move, LogBytesPerWord, G5_stack_move);
 878       __ add(Gargs, G5_stack_move, Gargs);
 879 
 880       // Compute the new Gargs (argslot[0]).
 881       Register O2_newarg = O2_scratch;
 882       __ mov(Gargs, O2_newarg);
 883 
 884       // Copy from oldarg[0...] down to newarg[0...]
 885       // Pseude-code:
 886       //   O1_oldarg  = old-Gargs
 887       //   O2_newarg  = new-Gargs
 888       //   O0_argslot = argslot
 889       //   while (O2_newarg < O1_oldarg) *O2_newarg = *O0_argslot++
 890       Label loop;
 891       __ bind(loop);
 892       __ ld_ptr(Address(O0_argslot, 0), O3_scratch);
 893       __ st_ptr(O3_scratch, Address(O2_newarg, 0));
 894       __ add(O0_argslot, wordSize, O0_argslot);
 895       __ add(O2_newarg,  wordSize, O2_newarg);
 896       __ cmp(O2_newarg, O1_oldarg);
 897       __ brx(Assembler::less, false, Assembler::pt, loop);
 898       __ delayed()->nop();  // FILLME
 899 
 900       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 901       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 902     }
 903     break;
 904 
 905   case _adapter_drop_args:
 906     {
 907       // 'argslot' is the position of the first argument to nuke.
 908       __ ldsw(G3_amh_vmargslot, O0_argslot);
 909       __ add(Gargs, __ argument_offset(O0_argslot), O0_argslot);
 910 
 911       // 'stack_move' is number of words to drop.
 912       Register G5_stack_move = G5_index;
 913       __ ldsw(G3_amh_conversion, G5_stack_move);
 914       __ sra(G5_stack_move, CONV_STACK_MOVE_SHIFT, G5_stack_move);
 915 
 916       remove_arg_slots(_masm, G5_stack_move, O0_argslot, O1_scratch, O2_scratch, O3_scratch);
 917 
 918       __ ld_ptr(G3_mh_vmtarget, G3_method_handle);
 919       __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
 920     }
 921     break;
 922 
 923   case _adapter_collect_args:
 924     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
 925     break;
 926 
 927   case _adapter_spread_args:
 928     // Handled completely by optimized cases.
 929     __ stop("init_AdapterMethodHandle should not issue this");
 930     break;
 931 
 932   case _adapter_opt_spread_0:
 933   case _adapter_opt_spread_1:
 934   case _adapter_opt_spread_more:
 935     {
 936       // spread an array out into a group of arguments
 937       __ unimplemented(entry_name(ek));
 938     }
 939     break;
 940 
 941   case _adapter_flyby:
 942   case _adapter_ricochet:
 943     __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
 944     break;
 945 
 946   default:
 947     ShouldNotReachHere();
 948   }
 949 
 950   address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
 951   __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
 952 
 953   init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
 954 }