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