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