1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009, 2010, 2011 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "interpreter/cppInterpreterGenerator.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "memory/allocation.inline.hpp"

  30 #include "oops/oop.inline.hpp"
  31 #include "prims/methodHandles.hpp"
  32 
  33 void MethodHandles::invoke_target(Method* method, TRAPS) {
  34 
  35   JavaThread *thread = (JavaThread *) THREAD;
  36   ZeroStack *stack = thread->zero_stack();
  37   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  38   interpreterState istate = frame->interpreter_state();
  39 
  40   // Trim back the stack to put the parameters at the top
  41   stack->set_sp(istate->stack() + 1);
  42 
  43   Interpreter::invoke_method(method, method->from_interpreted_entry(), THREAD);
  44 
  45   // Convert the result
  46   istate->set_stack(stack->sp() - 1);
  47 
  48 }
  49 
  50 oop MethodHandles::popFromStack(TRAPS) {
  51 
  52   JavaThread *thread = (JavaThread *) THREAD;
  53   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  54   interpreterState istate = frame->interpreter_state();
  55   intptr_t* topOfStack = istate->stack();
  56 
  57   oop top = STACK_OBJECT(-1);
  58   MORE_STACK(-1);
  59   istate->set_stack(topOfStack);
  60 
  61   return top;
  62 
  63 }
  64 
  65 int MethodHandles::method_handle_entry_invokeBasic(Method* method, intptr_t UNUSED, TRAPS) {
  66 
  67   JavaThread *thread = (JavaThread *) THREAD;
  68   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  69   interpreterState istate = frame->interpreter_state();
  70   intptr_t* topOfStack = istate->stack();
  71 
  72   // 'this' is a MethodHandle. We resolve the target method by accessing this.form.vmentry.vmtarget.
  73   int numArgs = method->size_of_parameters();
  74   oop lform1 = java_lang_invoke_MethodHandle::form(STACK_OBJECT(-numArgs)); // this.form
  75   oop vmEntry1 = java_lang_invoke_LambdaForm::vmentry(lform1);
  76   Method* vmtarget = (Method*) java_lang_invoke_MemberName::vmtarget(vmEntry1);
  77 
  78   invoke_target(vmtarget, THREAD);
  79 
  80   // No deoptimized frames on the stack
  81   return 0;
  82 }
  83 
  84 int MethodHandles::method_handle_entry_linkToStaticOrSpecial(Method* method, intptr_t UNUSED, TRAPS) {
  85 
  86   // Pop appendix argument from stack. This is a MemberName which we resolve to the
  87   // target method.
  88   oop vmentry = popFromStack(THREAD);
  89 
  90   Method* vmtarget = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
  91 
  92   invoke_target(vmtarget, THREAD);
  93 
  94   return 0;
  95 }
  96 
  97 int MethodHandles::method_handle_entry_linkToInterface(Method* method, intptr_t UNUSED, TRAPS) {
  98   JavaThread *thread = (JavaThread *) THREAD;
  99   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 100   interpreterState istate = frame->interpreter_state();
 101 
 102   // Pop appendix argument from stack. This is a MemberName which we resolve to the
 103   // target method.
 104   oop vmentry = popFromStack(THREAD);
 105   intptr_t* topOfStack = istate->stack();
 106 
 107   // Resolve target method by looking up in the receiver object's itable.
 108   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(vmentry));
 109   intptr_t vmindex = java_lang_invoke_MemberName::vmindex(vmentry);
 110   Method* target = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
 111 
 112   int numArgs = target->size_of_parameters();
 113   oop recv = STACK_OBJECT(-numArgs);
 114 
 115   InstanceKlass* klass_part = InstanceKlass::cast(recv->klass());
 116   itableOffsetEntry* ki = (itableOffsetEntry*) klass_part->start_of_itable();
 117   int i;
 118   for ( i = 0 ; i < klass_part->itable_length() ; i++, ki++ ) {
 119     if (ki->interface_klass() == clazz) break;
 120   }
 121 
 122   itableMethodEntry* im = ki->first_method_entry(recv->klass());
 123   Method* vmtarget = im[vmindex].method();
 124 
 125   invoke_target(vmtarget, THREAD);
 126 
 127   return 0;
 128 }
 129 
 130 int MethodHandles::method_handle_entry_linkToVirtual(Method* method, intptr_t UNUSED, TRAPS) {
 131   JavaThread *thread = (JavaThread *) THREAD;
 132 
 133   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 134   interpreterState istate = frame->interpreter_state();
 135 
 136   // Pop appendix argument from stack. This is a MemberName which we resolve to the
 137   // target method.
 138   oop vmentry = popFromStack(THREAD);
 139   intptr_t* topOfStack = istate->stack();
 140 
 141   // Resolve target method by looking up in the receiver object's vtable.
 142   intptr_t vmindex = java_lang_invoke_MemberName::vmindex(vmentry);
 143   Method* target = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
 144   int numArgs = target->size_of_parameters();
 145   oop recv = STACK_OBJECT(-numArgs);
 146   Klass* clazz = recv->klass();
 147   Klass* klass_part = InstanceKlass::cast(clazz);
 148   ResourceMark rm(THREAD);
 149   klassVtable* vtable = klass_part->vtable();
 150   Method* vmtarget = vtable->method_at(vmindex);
 151 
 152   invoke_target(vmtarget, THREAD);
 153 
 154   return 0;
 155 }
 156 
 157 int MethodHandles::method_handle_entry_invalid(Method* method, intptr_t UNUSED, TRAPS) {
 158   ShouldNotReachHere();
 159   return 0;
 160 }
 161 
 162 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* masm,
 163                                                                 vmIntrinsics::ID iid) {
 164   switch (iid) {
 165   case vmIntrinsics::_invokeGeneric:
 166   case vmIntrinsics::_compiledLambdaForm:
 167     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
 168     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
 169     // They all allow an appendix argument.
 170     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_invalid);
 171   case vmIntrinsics::_invokeBasic:
 172     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_invokeBasic);
 173   case vmIntrinsics::_linkToStatic:
 174   case vmIntrinsics::_linkToSpecial:
 175     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToStaticOrSpecial);
 176   case vmIntrinsics::_linkToInterface:
 177     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToInterface);
 178   case vmIntrinsics::_linkToVirtual:
 179     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToVirtual);
 180   default:
 181     ShouldNotReachHere();
 182     return NULL;
 183   }
 184 }
--- EOF ---