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