src/cpu/zero/vm/cppInterpreter_zero.cpp

Wed, 09 May 2012 00:28:45 -0400

author
dholmes
date
Wed, 09 May 2012 00:28:45 -0400
changeset 3774
7f410b6ea66c
parent 2978
d83ac25d0304
child 3969
1d7922586cf6
permissions
-rw-r--r--

7167406: (Zero) Fix for InvokeDynamic needed
Reviewed-by: chrisphi, dholmes
Contributed-by: Andrew Dinn <adinn@redhat.com>

     1 /*
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * Copyright 2007, 2008, 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  */
    26 #include "precompiled.hpp"
    27 #include "asm/assembler.hpp"
    28 #include "interpreter/bytecodeHistogram.hpp"
    29 #include "interpreter/cppInterpreter.hpp"
    30 #include "interpreter/interpreter.hpp"
    31 #include "interpreter/interpreterGenerator.hpp"
    32 #include "interpreter/interpreterRuntime.hpp"
    33 #include "oops/arrayOop.hpp"
    34 #include "oops/methodDataOop.hpp"
    35 #include "oops/methodOop.hpp"
    36 #include "oops/oop.inline.hpp"
    37 #include "prims/jvmtiExport.hpp"
    38 #include "prims/jvmtiThreadState.hpp"
    39 #include "runtime/arguments.hpp"
    40 #include "runtime/deoptimization.hpp"
    41 #include "runtime/frame.inline.hpp"
    42 #include "runtime/interfaceSupport.hpp"
    43 #include "runtime/sharedRuntime.hpp"
    44 #include "runtime/stubRoutines.hpp"
    45 #include "runtime/synchronizer.hpp"
    46 #include "runtime/timer.hpp"
    47 #include "runtime/vframeArray.hpp"
    48 #include "stack_zero.inline.hpp"
    49 #include "utilities/debug.hpp"
    50 #ifdef SHARK
    51 #include "shark/shark_globals.hpp"
    52 #endif
    54 #ifdef CC_INTERP
    56 #define fixup_after_potential_safepoint()       \
    57   method = istate->method()
    59 #define CALL_VM_NOCHECK_NOFIX(func)             \
    60   thread->set_last_Java_frame();                \
    61   func;                                         \
    62   thread->reset_last_Java_frame();
    64 #define CALL_VM_NOCHECK(func)                   \
    65   CALL_VM_NOCHECK_NOFIX(func)                   \
    66   fixup_after_potential_safepoint()
    68 int CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
    69   JavaThread *thread = (JavaThread *) THREAD;
    71   // Allocate and initialize our frame.
    72   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
    73   thread->push_zero_frame(frame);
    75   // Execute those bytecodes!
    76   main_loop(0, THREAD);
    78   // No deoptimized frames on the stack
    79   return 0;
    80 }
    82 void CppInterpreter::main_loop(int recurse, TRAPS) {
    83   JavaThread *thread = (JavaThread *) THREAD;
    84   ZeroStack *stack = thread->zero_stack();
    86   // If we are entering from a deopt we may need to call
    87   // ourself a few times in order to get to our frame.
    88   if (recurse)
    89     main_loop(recurse - 1, THREAD);
    91   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
    92   interpreterState istate = frame->interpreter_state();
    93   methodOop method = istate->method();
    95   intptr_t *result = NULL;
    96   int result_slots = 0;
    98   while (true) {
    99     // We can set up the frame anchor with everything we want at
   100     // this point as we are thread_in_Java and no safepoints can
   101     // occur until we go to vm mode.  We do have to clear flags
   102     // on return from vm but that is it.
   103     thread->set_last_Java_frame();
   105     // Call the interpreter
   106     if (JvmtiExport::can_post_interpreter_events())
   107       BytecodeInterpreter::runWithChecks(istate);
   108     else
   109       BytecodeInterpreter::run(istate);
   110     fixup_after_potential_safepoint();
   112     // Clear the frame anchor
   113     thread->reset_last_Java_frame();
   115     // Examine the message from the interpreter to decide what to do
   116     if (istate->msg() == BytecodeInterpreter::call_method) {
   117       methodOop callee = istate->callee();
   119       // Trim back the stack to put the parameters at the top
   120       stack->set_sp(istate->stack() + 1);
   122       // Make the call
   123       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
   124       fixup_after_potential_safepoint();
   126       // Convert the result
   127       istate->set_stack(stack->sp() - 1);
   129       // Restore the stack
   130       stack->set_sp(istate->stack_limit() + 1);
   132       // Resume the interpreter
   133       istate->set_msg(BytecodeInterpreter::method_resume);
   134     }
   135     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
   136       int monitor_words = frame::interpreter_frame_monitor_size();
   138       // Allocate the space
   139       stack->overflow_check(monitor_words, THREAD);
   140       if (HAS_PENDING_EXCEPTION)
   141         break;
   142       stack->alloc(monitor_words * wordSize);
   144       // Move the expression stack contents
   145       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
   146         *(p - monitor_words) = *p;
   148       // Move the expression stack pointers
   149       istate->set_stack_limit(istate->stack_limit() - monitor_words);
   150       istate->set_stack(istate->stack() - monitor_words);
   151       istate->set_stack_base(istate->stack_base() - monitor_words);
   153       // Zero the new monitor so the interpreter can find it.
   154       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
   156       // Resume the interpreter
   157       istate->set_msg(BytecodeInterpreter::got_monitors);
   158     }
   159     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
   160       // Copy the result into the caller's frame
   161       result_slots = type2size[result_type_of(method)];
   162       assert(result_slots >= 0 && result_slots <= 2, "what?");
   163       result = istate->stack() + result_slots;
   164       break;
   165     }
   166     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
   167       assert(HAS_PENDING_EXCEPTION, "should do");
   168       break;
   169     }
   170     else if (istate->msg() == BytecodeInterpreter::do_osr) {
   171       // Unwind the current frame
   172       thread->pop_zero_frame();
   174       // Remove any extension of the previous frame
   175       int extra_locals = method->max_locals() - method->size_of_parameters();
   176       stack->set_sp(stack->sp() + extra_locals);
   178       // Jump into the OSR method
   179       Interpreter::invoke_osr(
   180         method, istate->osr_entry(), istate->osr_buf(), THREAD);
   181       return;
   182     }
   183     else if (istate->msg() == BytecodeInterpreter::call_method_handle) {
   184       oop method_handle = istate->callee();
   186       // Trim back the stack to put the parameters at the top
   187       stack->set_sp(istate->stack() + 1);
   189       // Make the call
   190       process_method_handle(method_handle, THREAD);
   191       fixup_after_potential_safepoint();
   193       // Convert the result
   194       istate->set_stack(stack->sp() - 1);
   196       // Restore the stack
   197       stack->set_sp(istate->stack_limit() + 1);
   199       // Resume the interpreter
   200       istate->set_msg(BytecodeInterpreter::method_resume);
   201     }
   202     else {
   203       ShouldNotReachHere();
   204     }
   205   }
   207   // Unwind the current frame
   208   thread->pop_zero_frame();
   210   // Pop our local variables
   211   stack->set_sp(stack->sp() + method->max_locals());
   213   // Push our result
   214   for (int i = 0; i < result_slots; i++)
   215     stack->push(result[-i]);
   216 }
   218 int CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
   219   // Make sure method is native and not abstract
   220   assert(method->is_native() && !method->is_abstract(), "should be");
   222   JavaThread *thread = (JavaThread *) THREAD;
   223   ZeroStack *stack = thread->zero_stack();
   225   // Allocate and initialize our frame
   226   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
   227   thread->push_zero_frame(frame);
   228   interpreterState istate = frame->interpreter_state();
   229   intptr_t *locals = istate->locals();
   231   // Update the invocation counter
   232   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
   233     InvocationCounter *counter = method->invocation_counter();
   234     counter->increment();
   235     if (counter->reached_InvocationLimit()) {
   236       CALL_VM_NOCHECK(
   237         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
   238       if (HAS_PENDING_EXCEPTION)
   239         goto unwind_and_return;
   240     }
   241   }
   243   // Lock if necessary
   244   BasicObjectLock *monitor;
   245   monitor = NULL;
   246   if (method->is_synchronized()) {
   247     monitor = (BasicObjectLock*) istate->stack_base();
   248     oop lockee = monitor->obj();
   249     markOop disp = lockee->mark()->set_unlocked();
   251     monitor->lock()->set_displaced_header(disp);
   252     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
   253       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
   254         monitor->lock()->set_displaced_header(NULL);
   255       }
   256       else {
   257         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
   258         if (HAS_PENDING_EXCEPTION)
   259           goto unwind_and_return;
   260       }
   261     }
   262   }
   264   // Get the signature handler
   265   InterpreterRuntime::SignatureHandler *handler; {
   266     address handlerAddr = method->signature_handler();
   267     if (handlerAddr == NULL) {
   268       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
   269       if (HAS_PENDING_EXCEPTION)
   270         goto unlock_unwind_and_return;
   272       handlerAddr = method->signature_handler();
   273       assert(handlerAddr != NULL, "eh?");
   274     }
   275     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
   276       CALL_VM_NOCHECK(handlerAddr =
   277         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
   278       if (HAS_PENDING_EXCEPTION)
   279         goto unlock_unwind_and_return;
   280     }
   281     handler = \
   282       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
   283   }
   285   // Get the native function entry point
   286   address function;
   287   function = method->native_function();
   288   assert(function != NULL, "should be set if signature handler is");
   290   // Build the argument list
   291   stack->overflow_check(handler->argument_count() * 2, THREAD);
   292   if (HAS_PENDING_EXCEPTION)
   293     goto unlock_unwind_and_return;
   295   void **arguments;
   296   void *mirror; {
   297     arguments =
   298       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
   299     void **dst = arguments;
   301     void *env = thread->jni_environment();
   302     *(dst++) = &env;
   304     if (method->is_static()) {
   305       istate->set_oop_temp(
   306         method->constants()->pool_holder()->java_mirror());
   307       mirror = istate->oop_temp_addr();
   308       *(dst++) = &mirror;
   309     }
   311     intptr_t *src = locals;
   312     for (int i = dst - arguments; i < handler->argument_count(); i++) {
   313       ffi_type *type = handler->argument_type(i);
   314       if (type == &ffi_type_pointer) {
   315         if (*src) {
   316           stack->push((intptr_t) src);
   317           *(dst++) = stack->sp();
   318         }
   319         else {
   320           *(dst++) = src;
   321         }
   322         src--;
   323       }
   324       else if (type->size == 4) {
   325         *(dst++) = src--;
   326       }
   327       else if (type->size == 8) {
   328         src--;
   329         *(dst++) = src--;
   330       }
   331       else {
   332         ShouldNotReachHere();
   333       }
   334     }
   335   }
   337   // Set up the Java frame anchor
   338   thread->set_last_Java_frame();
   340   // Change the thread state to _thread_in_native
   341   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
   343   // Make the call
   344   intptr_t result[4 - LogBytesPerWord];
   345   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
   347   // Change the thread state back to _thread_in_Java.
   348   // ThreadStateTransition::transition_from_native() cannot be used
   349   // here because it does not check for asynchronous exceptions.
   350   // We have to manage the transition ourself.
   351   thread->set_thread_state(_thread_in_native_trans);
   353   // Make sure new state is visible in the GC thread
   354   if (os::is_MP()) {
   355     if (UseMembar) {
   356       OrderAccess::fence();
   357     }
   358     else {
   359       InterfaceSupport::serialize_memory(thread);
   360     }
   361   }
   363   // Handle safepoint operations, pending suspend requests,
   364   // and pending asynchronous exceptions.
   365   if (SafepointSynchronize::do_call_back() ||
   366       thread->has_special_condition_for_native_trans()) {
   367     JavaThread::check_special_condition_for_native_trans(thread);
   368     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
   369   }
   371   // Finally we can change the thread state to _thread_in_Java.
   372   thread->set_thread_state(_thread_in_Java);
   373   fixup_after_potential_safepoint();
   375   // Clear the frame anchor
   376   thread->reset_last_Java_frame();
   378   // If the result was an oop then unbox it and store it in
   379   // oop_temp where the garbage collector can see it before
   380   // we release the handle it might be protected by.
   381   if (handler->result_type() == &ffi_type_pointer) {
   382     if (result[0])
   383       istate->set_oop_temp(*(oop *) result[0]);
   384     else
   385       istate->set_oop_temp(NULL);
   386   }
   388   // Reset handle block
   389   thread->active_handles()->clear();
   391  unlock_unwind_and_return:
   393   // Unlock if necessary
   394   if (monitor) {
   395     BasicLock *lock = monitor->lock();
   396     markOop header = lock->displaced_header();
   397     oop rcvr = monitor->obj();
   398     monitor->set_obj(NULL);
   400     if (header != NULL) {
   401       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
   402         monitor->set_obj(rcvr); {
   403           HandleMark hm(thread);
   404           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
   405         }
   406       }
   407     }
   408   }
   410  unwind_and_return:
   412   // Unwind the current activation
   413   thread->pop_zero_frame();
   415   // Pop our parameters
   416   stack->set_sp(stack->sp() + method->size_of_parameters());
   418   // Push our result
   419   if (!HAS_PENDING_EXCEPTION) {
   420     BasicType type = result_type_of(method);
   421     stack->set_sp(stack->sp() - type2size[type]);
   423     switch (type) {
   424     case T_VOID:
   425       break;
   427     case T_BOOLEAN:
   428 #ifndef VM_LITTLE_ENDIAN
   429       result[0] <<= (BitsPerWord - BitsPerByte);
   430 #endif
   431       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
   432       break;
   434     case T_CHAR:
   435 #ifndef VM_LITTLE_ENDIAN
   436       result[0] <<= (BitsPerWord - BitsPerShort);
   437 #endif
   438       SET_LOCALS_INT(*(jchar *) result, 0);
   439       break;
   441     case T_BYTE:
   442 #ifndef VM_LITTLE_ENDIAN
   443       result[0] <<= (BitsPerWord - BitsPerByte);
   444 #endif
   445       SET_LOCALS_INT(*(jbyte *) result, 0);
   446       break;
   448     case T_SHORT:
   449 #ifndef VM_LITTLE_ENDIAN
   450       result[0] <<= (BitsPerWord - BitsPerShort);
   451 #endif
   452       SET_LOCALS_INT(*(jshort *) result, 0);
   453       break;
   455     case T_INT:
   456 #ifndef VM_LITTLE_ENDIAN
   457       result[0] <<= (BitsPerWord - BitsPerInt);
   458 #endif
   459       SET_LOCALS_INT(*(jint *) result, 0);
   460       break;
   462     case T_LONG:
   463       SET_LOCALS_LONG(*(jlong *) result, 0);
   464       break;
   466     case T_FLOAT:
   467       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
   468       break;
   470     case T_DOUBLE:
   471       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
   472       break;
   474     case T_OBJECT:
   475     case T_ARRAY:
   476       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
   477       break;
   479     default:
   480       ShouldNotReachHere();
   481     }
   482   }
   484   // No deoptimized frames on the stack
   485   return 0;
   486 }
   488 int CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
   489   JavaThread *thread = (JavaThread *) THREAD;
   490   ZeroStack *stack = thread->zero_stack();
   491   intptr_t *locals = stack->sp();
   493   // Drop into the slow path if we need a safepoint check
   494   if (SafepointSynchronize::do_call_back()) {
   495     return normal_entry(method, 0, THREAD);
   496   }
   498   // Load the object pointer and drop into the slow path
   499   // if we have a NullPointerException
   500   oop object = LOCALS_OBJECT(0);
   501   if (object == NULL) {
   502     return normal_entry(method, 0, THREAD);
   503   }
   505   // Read the field index from the bytecode, which looks like this:
   506   //  0:  aload_0
   507   //  1:  getfield
   508   //  2:    index
   509   //  3:    index
   510   //  4:  ireturn/areturn
   511   // NB this is not raw bytecode: index is in machine order
   512   u1 *code = method->code_base();
   513   assert(code[0] == Bytecodes::_aload_0 &&
   514          code[1] == Bytecodes::_getfield &&
   515          (code[4] == Bytecodes::_ireturn ||
   516           code[4] == Bytecodes::_areturn), "should do");
   517   u2 index = Bytes::get_native_u2(&code[2]);
   519   // Get the entry from the constant pool cache, and drop into
   520   // the slow path if it has not been resolved
   521   constantPoolCacheOop cache = method->constants()->cache();
   522   ConstantPoolCacheEntry* entry = cache->entry_at(index);
   523   if (!entry->is_resolved(Bytecodes::_getfield)) {
   524     return normal_entry(method, 0, THREAD);
   525   }
   527   // Get the result and push it onto the stack
   528   switch (entry->flag_state()) {
   529   case ltos:
   530   case dtos:
   531     stack->overflow_check(1, CHECK_0);
   532     stack->alloc(wordSize);
   533     break;
   534   }
   535   if (entry->is_volatile()) {
   536     switch (entry->flag_state()) {
   537     case ctos:
   538       SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
   539       break;
   541     case btos:
   542       SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
   543       break;
   545     case stos:
   546       SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
   547       break;
   549     case itos:
   550       SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
   551       break;
   553     case ltos:
   554       SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
   555       break;
   557     case ftos:
   558       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
   559       break;
   561     case dtos:
   562       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
   563       break;
   565     case atos:
   566       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
   567       break;
   569     default:
   570       ShouldNotReachHere();
   571     }
   572   }
   573   else {
   574     switch (entry->flag_state()) {
   575     case ctos:
   576       SET_LOCALS_INT(object->char_field(entry->f2()), 0);
   577       break;
   579     case btos:
   580       SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
   581       break;
   583     case stos:
   584       SET_LOCALS_INT(object->short_field(entry->f2()), 0);
   585       break;
   587     case itos:
   588       SET_LOCALS_INT(object->int_field(entry->f2()), 0);
   589       break;
   591     case ltos:
   592       SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
   593       break;
   595     case ftos:
   596       SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
   597       break;
   599     case dtos:
   600       SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
   601       break;
   603     case atos:
   604       SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
   605       break;
   607     default:
   608       ShouldNotReachHere();
   609     }
   610   }
   612   // No deoptimized frames on the stack
   613   return 0;
   614 }
   616 int CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
   617   JavaThread *thread = (JavaThread *) THREAD;
   618   ZeroStack *stack = thread->zero_stack();
   620   // Drop into the slow path if we need a safepoint check
   621   if (SafepointSynchronize::do_call_back()) {
   622     return normal_entry(method, 0, THREAD);
   623   }
   625   // Pop our parameters
   626   stack->set_sp(stack->sp() + method->size_of_parameters());
   628   // No deoptimized frames on the stack
   629   return 0;
   630 }
   632 int CppInterpreter::method_handle_entry(methodOop method,
   633                                         intptr_t UNUSED, TRAPS) {
   634   JavaThread *thread = (JavaThread *) THREAD;
   635   ZeroStack *stack = thread->zero_stack();
   636   int argument_slots = method->size_of_parameters();
   637   int result_slots = type2size[result_type_of(method)];
   638   intptr_t *vmslots = stack->sp();
   639   intptr_t *unwind_sp = vmslots + argument_slots;
   641   // Find the MethodType
   642   address p = (address) method;
   643   for (jint* pc = method->method_type_offsets_chain(); (*pc) != -1; pc++) {
   644     p = *(address*)(p + (*pc));
   645   }
   646   oop method_type = (oop) p;
   648   // The MethodHandle is in the slot after the arguments
   649   oop form = java_lang_invoke_MethodType::form(method_type);
   650   int num_vmslots = java_lang_invoke_MethodTypeForm::vmslots(form);
   651   assert(argument_slots == num_vmslots + 1, "should be");
   652   oop method_handle = VMSLOTS_OBJECT(num_vmslots);
   654   // InvokeGeneric requires some extra shuffling
   655   oop mhtype = java_lang_invoke_MethodHandle::type(method_handle);
   656   bool is_exact = mhtype == method_type;
   657   if (!is_exact) {
   658     if (method->intrinsic_id() == vmIntrinsics::_invokeExact) {
   659       CALL_VM_NOCHECK_NOFIX(
   660         SharedRuntime::throw_WrongMethodTypeException(
   661           thread, method_type, mhtype));
   662       // NB all oops trashed!
   663       assert(HAS_PENDING_EXCEPTION, "should do");
   664       stack->set_sp(unwind_sp);
   665       return 0;
   666     }
   667     assert(method->intrinsic_id() == vmIntrinsics::_invokeGeneric, "should be");
   669     // Load up an adapter from the calling type
   670     // NB the x86 code for this (in methodHandles_x86.cpp, search for
   671     // "genericInvoker") is really really odd.  I'm hoping it's trying
   672     // to accomodate odd VM/class library combinations I can ignore.
   673     oop adapter = java_lang_invoke_MethodTypeForm::genericInvoker(form);
   674     if (adapter == NULL) {
   675       CALL_VM_NOCHECK_NOFIX(
   676         SharedRuntime::throw_WrongMethodTypeException(
   677           thread, method_type, mhtype));
   678       // NB all oops trashed!
   679       assert(HAS_PENDING_EXCEPTION, "should do");
   680       stack->set_sp(unwind_sp);
   681       return 0;
   682     }
   684     // Adapters are shared among form-families of method-type.  The
   685     // type being called is passed as a trusted first argument so that
   686     // the adapter knows the actual types of its arguments and return
   687     // values.
   688     insert_vmslots(num_vmslots + 1, 1, THREAD);
   689     if (HAS_PENDING_EXCEPTION) {
   690       // NB all oops trashed!
   691       stack->set_sp(unwind_sp);
   692       return 0;
   693     }
   695     vmslots = stack->sp();
   696     num_vmslots++;
   697     SET_VMSLOTS_OBJECT(method_type, num_vmslots);
   699     method_handle = adapter;
   700   }
   702   // Start processing
   703   process_method_handle(method_handle, THREAD);
   704   if (HAS_PENDING_EXCEPTION)
   705     result_slots = 0;
   707   // If this is an invokeExact then the eventual callee will not
   708   // have unwound the method handle argument so we have to do it.
   709   // If a result is being returned the it will be above the method
   710   // handle argument we're unwinding.
   711   if (is_exact) {
   712     intptr_t result[2];
   713     for (int i = 0; i < result_slots; i++)
   714       result[i] = stack->pop();
   715     stack->pop();
   716     for (int i = result_slots - 1; i >= 0; i--)
   717       stack->push(result[i]);
   718   }
   720   // Check
   721   assert(stack->sp() == unwind_sp - result_slots, "should be");
   723   // No deoptimized frames on the stack
   724   return 0;
   725 }
   727 void CppInterpreter::process_method_handle(oop method_handle, TRAPS) {
   728   JavaThread *thread = (JavaThread *) THREAD;
   729   ZeroStack *stack = thread->zero_stack();
   730   intptr_t *vmslots = stack->sp();
   732   bool direct_to_method = false;
   733   BasicType src_rtype = T_ILLEGAL;
   734   BasicType dst_rtype = T_ILLEGAL;
   736   MethodHandleEntry *entry =
   737     java_lang_invoke_MethodHandle::vmentry(method_handle);
   738   MethodHandles::EntryKind entry_kind =
   739     (MethodHandles::EntryKind) (((intptr_t) entry) & 0xffffffff);
   741   methodOop method = NULL;
   742   switch (entry_kind) {
   743   case MethodHandles::_invokestatic_mh:
   744     direct_to_method = true;
   745     break;
   747   case MethodHandles::_invokespecial_mh:
   748   case MethodHandles::_invokevirtual_mh:
   749   case MethodHandles::_invokeinterface_mh:
   750     {
   751       oop receiver =
   752         VMSLOTS_OBJECT(
   753           java_lang_invoke_MethodHandle::vmslots(method_handle) - 1);
   754       if (receiver == NULL) {
   755           stack->set_sp(calculate_unwind_sp(stack, method_handle));
   756           CALL_VM_NOCHECK_NOFIX(
   757             throw_exception(
   758               thread, vmSymbols::java_lang_NullPointerException()));
   759           // NB all oops trashed!
   760           assert(HAS_PENDING_EXCEPTION, "should do");
   761           return;
   762       }
   763       if (entry_kind != MethodHandles::_invokespecial_mh) {
   764         int index = java_lang_invoke_DirectMethodHandle::vmindex(method_handle);
   765         instanceKlass* rcvrKlass =
   766           (instanceKlass *) receiver->klass()->klass_part();
   767         if (entry_kind == MethodHandles::_invokevirtual_mh) {
   768           method = (methodOop) rcvrKlass->start_of_vtable()[index];
   769         }
   770         else {
   771           oop iclass = java_lang_invoke_MethodHandle::vmtarget(method_handle);
   772           itableOffsetEntry* ki =
   773             (itableOffsetEntry *) rcvrKlass->start_of_itable();
   774           int i, length = rcvrKlass->itable_length();
   775           for (i = 0; i < length; i++, ki++ ) {
   776             if (ki->interface_klass() == iclass)
   777               break;
   778           }
   779           if (i == length) {
   780             stack->set_sp(calculate_unwind_sp(stack, method_handle));
   781             CALL_VM_NOCHECK_NOFIX(
   782               throw_exception(
   783                 thread, vmSymbols::java_lang_IncompatibleClassChangeError()));
   784             // NB all oops trashed!
   785             assert(HAS_PENDING_EXCEPTION, "should do");
   786             return;
   787           }
   788           itableMethodEntry* im = ki->first_method_entry(receiver->klass());
   789           method = im[index].method();
   790           if (method == NULL) {
   791             stack->set_sp(calculate_unwind_sp(stack, method_handle));
   792             CALL_VM_NOCHECK_NOFIX(
   793               throw_exception(
   794                 thread, vmSymbols::java_lang_AbstractMethodError()));
   795             // NB all oops trashed!
   796             assert(HAS_PENDING_EXCEPTION, "should do");
   797             return;
   798           }
   799         }
   800       }
   801     }
   802     direct_to_method = true;
   803     break;
   805   case MethodHandles::_bound_ref_direct_mh:
   806   case MethodHandles::_bound_int_direct_mh:
   807   case MethodHandles::_bound_long_direct_mh:
   808     direct_to_method = true;
   809     // fall through
   810   case MethodHandles::_bound_ref_mh:
   811   case MethodHandles::_bound_int_mh:
   812   case MethodHandles::_bound_long_mh:
   813     {
   814       BasicType arg_type  = T_ILLEGAL;
   815       int       arg_mask  = -1;
   816       int       arg_slots = -1;
   817       MethodHandles::get_ek_bound_mh_info(
   818         entry_kind, arg_type, arg_mask, arg_slots);
   819       int arg_slot =
   820         java_lang_invoke_BoundMethodHandle::vmargslot(method_handle);
   822       // Create the new slot(s)
   823       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
   824       insert_vmslots(arg_slot, arg_slots, THREAD);
   825       if (HAS_PENDING_EXCEPTION) {
   826         // all oops trashed
   827         stack->set_sp(unwind_sp);
   828         return;
   829       }
   830       vmslots = stack->sp();
   832       // Store bound argument into new stack slot
   833       oop arg = java_lang_invoke_BoundMethodHandle::argument(method_handle);
   834       if (arg_type == T_OBJECT) {
   835         assert(arg_slots == 1, "should be");
   836         SET_VMSLOTS_OBJECT(arg, arg_slot);
   837       }
   838       else {
   839         jvalue arg_value;
   840         arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
   841         switch (arg_type) {
   842         case T_BOOLEAN:
   843           SET_VMSLOTS_INT(arg_value.z, arg_slot);
   844           break;
   845         case T_CHAR:
   846           SET_VMSLOTS_INT(arg_value.c, arg_slot);
   847           break;
   848         case T_BYTE:
   849           SET_VMSLOTS_INT(arg_value.b, arg_slot);
   850           break;
   851         case T_SHORT:
   852           SET_VMSLOTS_INT(arg_value.s, arg_slot);
   853           break;
   854         case T_INT:
   855           SET_VMSLOTS_INT(arg_value.i, arg_slot);
   856           break;
   857         case T_FLOAT:
   858           SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
   859           break;
   860         case T_LONG:
   861           SET_VMSLOTS_LONG(arg_value.j, arg_slot + 1);
   862           break;
   863         case T_DOUBLE:
   864           SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot + 1);
   865           break;
   866         default:
   867           tty->print_cr("unhandled type %s", type2name(arg_type));
   868           ShouldNotReachHere();
   869         }
   870       }
   871     }
   872     break;
   874   case MethodHandles::_adapter_retype_only:
   875   case MethodHandles::_adapter_retype_raw:
   876     src_rtype = result_type_of_handle(
   877       java_lang_invoke_MethodHandle::vmtarget(method_handle));
   878     dst_rtype = result_type_of_handle(method_handle);
   879     break;
   881   case MethodHandles::_adapter_check_cast:
   882     {
   883       int arg_slot =
   884         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
   885       oop arg = VMSLOTS_OBJECT(arg_slot);
   886       if (arg != NULL) {
   887         klassOop objKlassOop = arg->klass();
   888         klassOop klassOf = java_lang_Class::as_klassOop(
   889           java_lang_invoke_AdapterMethodHandle::argument(method_handle));
   891         if (objKlassOop != klassOf &&
   892             !objKlassOop->klass_part()->is_subtype_of(klassOf)) {
   893           ResourceMark rm(THREAD);
   894           const char* objName = Klass::cast(objKlassOop)->external_name();
   895           const char* klassName = Klass::cast(klassOf)->external_name();
   896           char* message = SharedRuntime::generate_class_cast_message(
   897             objName, klassName);
   899           stack->set_sp(calculate_unwind_sp(stack, method_handle));
   900           CALL_VM_NOCHECK_NOFIX(
   901             throw_exception(
   902               thread, vmSymbols::java_lang_ClassCastException(), message));
   903           // NB all oops trashed!
   904           assert(HAS_PENDING_EXCEPTION, "should do");
   905           return;
   906         }
   907       }
   908     }
   909     break;
   911   case MethodHandles::_adapter_dup_args:
   912     {
   913       int arg_slot =
   914         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
   915       int conv =
   916         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
   917       int num_slots = -MethodHandles::adapter_conversion_stack_move(conv);
   918       assert(num_slots > 0, "should be");
   920       // Create the new slot(s)
   921       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
   922       stack->overflow_check(num_slots, THREAD);
   923       if (HAS_PENDING_EXCEPTION) {
   924         // all oops trashed
   925         stack->set_sp(unwind_sp);
   926         return;
   927       }
   929       // Duplicate the arguments
   930       for (int i = num_slots - 1; i >= 0; i--)
   931         stack->push(*VMSLOTS_SLOT(arg_slot + i));
   933       vmslots = stack->sp(); // unused, but let the compiler figure that out
   934     }
   935     break;
   937   case MethodHandles::_adapter_drop_args:
   938     {
   939       int arg_slot =
   940         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
   941       int conv =
   942         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
   943       int num_slots = MethodHandles::adapter_conversion_stack_move(conv);
   944       assert(num_slots > 0, "should be");
   946       remove_vmslots(arg_slot, num_slots, THREAD); // doesn't trap
   947       vmslots = stack->sp(); // unused, but let the compiler figure that out
   948     }
   949     break;
   951   case MethodHandles::_adapter_opt_swap_1:
   952   case MethodHandles::_adapter_opt_swap_2:
   953   case MethodHandles::_adapter_opt_rot_1_up:
   954   case MethodHandles::_adapter_opt_rot_1_down:
   955   case MethodHandles::_adapter_opt_rot_2_up:
   956   case MethodHandles::_adapter_opt_rot_2_down:
   957     {
   958       int arg1 =
   959         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
   960       int conv =
   961         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
   962       int arg2 = MethodHandles::adapter_conversion_vminfo(conv);
   964       int swap_bytes = 0, rotate = 0;
   965       MethodHandles::get_ek_adapter_opt_swap_rot_info(
   966         entry_kind, swap_bytes, rotate);
   967       int swap_slots = swap_bytes >> LogBytesPerWord;
   969       intptr_t tmp;
   970       switch (rotate) {
   971       case 0: // swap
   972         for (int i = 0; i < swap_slots; i++) {
   973           tmp = *VMSLOTS_SLOT(arg1 + i);
   974           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(arg2 + i), arg1 + i);
   975           SET_VMSLOTS_SLOT(&tmp, arg2 + i);
   976         }
   977         break;
   979       case 1: // up
   980         assert(arg1 - swap_slots > arg2, "should be");
   982         tmp = *VMSLOTS_SLOT(arg1);
   983         for (int i = arg1 - swap_slots; i >= arg2; i--)
   984           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + swap_slots);
   985         SET_VMSLOTS_SLOT(&tmp, arg2);
   987         break;
   989       case -1: // down
   990         assert(arg2 - swap_slots > arg1, "should be");
   992         tmp = *VMSLOTS_SLOT(arg1);
   993         for (int i = arg1 + swap_slots; i <= arg2; i++)
   994           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i - swap_slots);
   995         SET_VMSLOTS_SLOT(&tmp, arg2);
   996         break;
   998       default:
   999         ShouldNotReachHere();
  1002     break;
  1004   case MethodHandles::_adapter_opt_i2l:
  1006       int arg_slot =
  1007         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
  1008       int arg = VMSLOTS_INT(arg_slot);
  1009       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
  1010       insert_vmslots(arg_slot, 1, THREAD);
  1011       if (HAS_PENDING_EXCEPTION) {
  1012         // all oops trashed
  1013         stack->set_sp(unwind_sp);
  1014         return;
  1016       vmslots = stack->sp();
  1017       arg_slot++;
  1018       SET_VMSLOTS_LONG(arg, arg_slot);
  1020     break;
  1022   case MethodHandles::_adapter_opt_unboxi:
  1023   case MethodHandles::_adapter_opt_unboxl:
  1025       int arg_slot =
  1026         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
  1027       oop arg = VMSLOTS_OBJECT(arg_slot);
  1028       jvalue arg_value;
  1029       if (arg == NULL) {
  1030         // queue a nullpointer exception for the caller
  1031         stack->set_sp(calculate_unwind_sp(stack, method_handle));
  1032         CALL_VM_NOCHECK_NOFIX(
  1033           throw_exception(
  1034             thread, vmSymbols::java_lang_NullPointerException()));
  1035         // NB all oops trashed!
  1036         assert(HAS_PENDING_EXCEPTION, "should do");
  1037         return;
  1039       BasicType arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
  1040       if (arg_type == T_LONG || arg_type == T_DOUBLE) {
  1041         intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
  1042         insert_vmslots(arg_slot, 1, THREAD);
  1043         if (HAS_PENDING_EXCEPTION) {
  1044           // all oops trashed
  1045           stack->set_sp(unwind_sp);
  1046           return;
  1048         vmslots = stack->sp();
  1049         arg_slot++;
  1051       switch (arg_type) {
  1052       case T_BOOLEAN:
  1053         SET_VMSLOTS_INT(arg_value.z, arg_slot);
  1054         break;
  1055       case T_CHAR:
  1056         SET_VMSLOTS_INT(arg_value.c, arg_slot);
  1057         break;
  1058       case T_BYTE:
  1059         SET_VMSLOTS_INT(arg_value.b, arg_slot);
  1060         break;
  1061       case T_SHORT:
  1062         SET_VMSLOTS_INT(arg_value.s, arg_slot);
  1063         break;
  1064       case T_INT:
  1065         SET_VMSLOTS_INT(arg_value.i, arg_slot);
  1066         break;
  1067       case T_FLOAT:
  1068         SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
  1069         break;
  1070       case T_LONG:
  1071         SET_VMSLOTS_LONG(arg_value.j, arg_slot);
  1072         break;
  1073       case T_DOUBLE:
  1074         SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot);
  1075         break;
  1076       default:
  1077         tty->print_cr("unhandled type %s", type2name(arg_type));
  1078         ShouldNotReachHere();
  1081     break;
  1083   default:
  1084     tty->print_cr("unhandled entry_kind %s",
  1085                   MethodHandles::entry_name(entry_kind));
  1086     ShouldNotReachHere();
  1089   // Continue along the chain
  1090   if (direct_to_method) {
  1091     if (method == NULL) {
  1092       method =
  1093         (methodOop) java_lang_invoke_MethodHandle::vmtarget(method_handle);
  1095     address entry_point = method->from_interpreted_entry();
  1096     Interpreter::invoke_method(method, entry_point, THREAD);
  1098   else {
  1099     process_method_handle(
  1100       java_lang_invoke_MethodHandle::vmtarget(method_handle), THREAD);
  1102   // NB all oops now trashed
  1104   // Adapt the result type, if necessary
  1105   if (src_rtype != dst_rtype && !HAS_PENDING_EXCEPTION) {
  1106     switch (dst_rtype) {
  1107     case T_VOID:
  1108       for (int i = 0; i < type2size[src_rtype]; i++)
  1109         stack->pop();
  1110       return;
  1112     case T_INT:
  1113       switch (src_rtype) {
  1114       case T_VOID:
  1115         stack->overflow_check(1, CHECK);
  1116         stack->push(0);
  1117         return;
  1119       case T_BOOLEAN:
  1120       case T_CHAR:
  1121       case T_BYTE:
  1122       case T_SHORT:
  1123         return;
  1125       // INT results sometimes need narrowing
  1126     case T_BOOLEAN:
  1127     case T_CHAR:
  1128     case T_BYTE:
  1129     case T_SHORT:
  1130       switch (src_rtype) {
  1131       case T_INT:
  1132         return;
  1136     tty->print_cr("unhandled conversion:");
  1137     tty->print_cr("src_rtype = %s", type2name(src_rtype));
  1138     tty->print_cr("dst_rtype = %s", type2name(dst_rtype));
  1139     ShouldNotReachHere();
  1143 // The new slots will be inserted before slot insert_before.
  1144 // Slots < insert_before will have the same slot number after the insert.
  1145 // Slots >= insert_before will become old_slot + num_slots.
  1146 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
  1147   JavaThread *thread = (JavaThread *) THREAD;
  1148   ZeroStack *stack = thread->zero_stack();
  1150   // Allocate the space
  1151   stack->overflow_check(num_slots, CHECK);
  1152   stack->alloc(num_slots * wordSize);
  1153   intptr_t *vmslots = stack->sp();
  1155   // Shuffle everything up
  1156   for (int i = 0; i < insert_before; i++)
  1157     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
  1160 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
  1161   JavaThread *thread = (JavaThread *) THREAD;
  1162   ZeroStack *stack = thread->zero_stack();
  1163   intptr_t *vmslots = stack->sp();
  1165   // Move everything down
  1166   for (int i = first_slot - 1; i >= 0; i--)
  1167     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
  1169   // Deallocate the space
  1170   stack->set_sp(stack->sp() + num_slots);
  1173 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
  1174   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
  1175   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
  1176   return java_lang_Class::as_BasicType(return_type, (klassOop *) NULL);
  1179 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
  1180                                               oop method_handle) {
  1181   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
  1182   oop form = java_lang_invoke_MethodType::form(method_type);
  1183   int argument_slots = java_lang_invoke_MethodTypeForm::vmslots(form);
  1185   return stack->sp() + argument_slots;
  1188 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
  1189                                                 Symbol*     name,
  1190                                                 char*       message))
  1191   THROW_MSG(name, message);
  1192 IRT_END
  1194 InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) {
  1195   JavaThread *thread = (JavaThread *) THREAD;
  1196   ZeroStack *stack = thread->zero_stack();
  1198   // Calculate the size of the frame we'll build, including
  1199   // any adjustments to the caller's frame that we'll make.
  1200   int extra_locals  = 0;
  1201   int monitor_words = 0;
  1202   int stack_words   = 0;
  1204   if (!method->is_native()) {
  1205     extra_locals = method->max_locals() - method->size_of_parameters();
  1206     stack_words  = method->max_stack();
  1208   if (method->is_synchronized()) {
  1209     monitor_words = frame::interpreter_frame_monitor_size();
  1211   stack->overflow_check(
  1212     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
  1214   // Adjust the caller's stack frame to accomodate any additional
  1215   // local variables we have contiguously with our parameters.
  1216   for (int i = 0; i < extra_locals; i++)
  1217     stack->push(0);
  1219   intptr_t *locals;
  1220   if (method->is_native())
  1221     locals = stack->sp() + (method->size_of_parameters() - 1);
  1222   else
  1223     locals = stack->sp() + (method->max_locals() - 1);
  1225   stack->push(0); // next_frame, filled in later
  1226   intptr_t *fp = stack->sp();
  1227   assert(fp - stack->sp() == next_frame_off, "should be");
  1229   stack->push(INTERPRETER_FRAME);
  1230   assert(fp - stack->sp() == frame_type_off, "should be");
  1232   interpreterState istate =
  1233     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
  1234   assert(fp - stack->sp() == istate_off, "should be");
  1236   istate->set_locals(locals);
  1237   istate->set_method(method);
  1238   istate->set_self_link(istate);
  1239   istate->set_prev_link(NULL);
  1240   istate->set_thread(thread);
  1241   istate->set_bcp(method->is_native() ? NULL : method->code_base());
  1242   istate->set_constants(method->constants()->cache());
  1243   istate->set_msg(BytecodeInterpreter::method_entry);
  1244   istate->set_oop_temp(NULL);
  1245   istate->set_mdx(NULL);
  1246   istate->set_callee(NULL);
  1248   istate->set_monitor_base((BasicObjectLock *) stack->sp());
  1249   if (method->is_synchronized()) {
  1250     BasicObjectLock *monitor =
  1251       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
  1252     oop object;
  1253     if (method->is_static())
  1254       object = method->constants()->pool_holder()->java_mirror();
  1255     else
  1256       object = (oop) locals[0];
  1257     monitor->set_obj(object);
  1260   istate->set_stack_base(stack->sp());
  1261   istate->set_stack(stack->sp() - 1);
  1262   if (stack_words)
  1263     stack->alloc(stack_words * wordSize);
  1264   istate->set_stack_limit(stack->sp() - 1);
  1266   return (InterpreterFrame *) fp;
  1269 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  1270   int i = 0;
  1271   switch (type) {
  1272     case T_BOOLEAN: i = 0; break;
  1273     case T_CHAR   : i = 1; break;
  1274     case T_BYTE   : i = 2; break;
  1275     case T_SHORT  : i = 3; break;
  1276     case T_INT    : i = 4; break;
  1277     case T_LONG   : i = 5; break;
  1278     case T_VOID   : i = 6; break;
  1279     case T_FLOAT  : i = 7; break;
  1280     case T_DOUBLE : i = 8; break;
  1281     case T_OBJECT : i = 9; break;
  1282     case T_ARRAY  : i = 9; break;
  1283     default       : ShouldNotReachHere();
  1285   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
  1286          "index out of bounds");
  1287   return i;
  1290 BasicType CppInterpreter::result_type_of(methodOop method) {
  1291   BasicType t;
  1292   switch (method->result_index()) {
  1293     case 0 : t = T_BOOLEAN; break;
  1294     case 1 : t = T_CHAR;    break;
  1295     case 2 : t = T_BYTE;    break;
  1296     case 3 : t = T_SHORT;   break;
  1297     case 4 : t = T_INT;     break;
  1298     case 5 : t = T_LONG;    break;
  1299     case 6 : t = T_VOID;    break;
  1300     case 7 : t = T_FLOAT;   break;
  1301     case 8 : t = T_DOUBLE;  break;
  1302     case 9 : t = T_OBJECT;  break;
  1303     default: ShouldNotReachHere();
  1305   assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
  1306          "out of step with AbstractInterpreter::BasicType_as_index");
  1307   return t;
  1310 address InterpreterGenerator::generate_empty_entry() {
  1311   if (!UseFastEmptyMethods)
  1312     return NULL;
  1314   return generate_entry((address) CppInterpreter::empty_entry);
  1317 address InterpreterGenerator::generate_accessor_entry() {
  1318   if (!UseFastAccessorMethods)
  1319     return NULL;
  1321   return generate_entry((address) CppInterpreter::accessor_entry);
  1324 address InterpreterGenerator::generate_Reference_get_entry(void) {
  1325 #ifndef SERIALGC
  1326   if (UseG1GC) {
  1327     // We need to generate have a routine that generates code to:
  1328     //   * load the value in the referent field
  1329     //   * passes that value to the pre-barrier.
  1330     //
  1331     // In the case of G1 this will record the value of the
  1332     // referent in an SATB buffer if marking is active.
  1333     // This will cause concurrent marking to mark the referent
  1334     // field as live.
  1335     Unimplemented();
  1337 #endif // SERIALGC
  1339   // If G1 is not enabled then attempt to go through the accessor entry point
  1340   // Reference.get is an accessor
  1341   return generate_accessor_entry();
  1344 address InterpreterGenerator::generate_native_entry(bool synchronized) {
  1345   assert(synchronized == false, "should be");
  1347   return generate_entry((address) CppInterpreter::native_entry);
  1350 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
  1351   assert(synchronized == false, "should be");
  1353   return generate_entry((address) CppInterpreter::normal_entry);
  1356 address AbstractInterpreterGenerator::generate_method_entry(
  1357     AbstractInterpreter::MethodKind kind) {
  1358   address entry_point = NULL;
  1360   switch (kind) {
  1361   case Interpreter::zerolocals:
  1362   case Interpreter::zerolocals_synchronized:
  1363     break;
  1365   case Interpreter::native:
  1366     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
  1367     break;
  1369   case Interpreter::native_synchronized:
  1370     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
  1371     break;
  1373   case Interpreter::empty:
  1374     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
  1375     break;
  1377   case Interpreter::accessor:
  1378     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
  1379     break;
  1381   case Interpreter::abstract:
  1382     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
  1383     break;
  1385   case Interpreter::method_handle:
  1386     entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
  1387     break;
  1389   case Interpreter::java_lang_math_sin:
  1390   case Interpreter::java_lang_math_cos:
  1391   case Interpreter::java_lang_math_tan:
  1392   case Interpreter::java_lang_math_abs:
  1393   case Interpreter::java_lang_math_log:
  1394   case Interpreter::java_lang_math_log10:
  1395   case Interpreter::java_lang_math_sqrt:
  1396     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
  1397     break;
  1399   case Interpreter::java_lang_ref_reference_get:
  1400     entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
  1401     break;
  1403   default:
  1404     ShouldNotReachHere();
  1407   if (entry_point == NULL)
  1408     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
  1410   return entry_point;
  1413 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
  1414  : CppInterpreterGenerator(code) {
  1415    generate_all();
  1418 // Deoptimization helpers
  1420 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
  1421   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
  1423   int size_in_words = size >> LogBytesPerWord;
  1424   assert(size_in_words * wordSize == size, "unaligned");
  1425   assert(size_in_words >= header_words, "too small");
  1426   stack->overflow_check(size_in_words, CHECK_NULL);
  1428   stack->push(0); // next_frame, filled in later
  1429   intptr_t *fp = stack->sp();
  1430   assert(fp - stack->sp() == next_frame_off, "should be");
  1432   stack->push(INTERPRETER_FRAME);
  1433   assert(fp - stack->sp() == frame_type_off, "should be");
  1435   interpreterState istate =
  1436     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
  1437   assert(fp - stack->sp() == istate_off, "should be");
  1438   istate->set_self_link(NULL); // mark invalid
  1440   stack->alloc((size_in_words - header_words) * wordSize);
  1442   return (InterpreterFrame *) fp;
  1445 int AbstractInterpreter::layout_activation(methodOop method,
  1446                                            int       tempcount,
  1447                                            int       popframe_extra_args,
  1448                                            int       moncount,
  1449                                            int       caller_actual_parameters,
  1450                                            int       callee_param_count,
  1451                                            int       callee_locals,
  1452                                            frame*    caller,
  1453                                            frame*    interpreter_frame,
  1454                                            bool      is_top_frame) {
  1455   assert(popframe_extra_args == 0, "what to do?");
  1456   assert(!is_top_frame || (!callee_locals && !callee_param_count),
  1457          "top frame should have no caller");
  1459   // This code must exactly match what InterpreterFrame::build
  1460   // does (the full InterpreterFrame::build, that is, not the
  1461   // one that creates empty frames for the deoptimizer).
  1462   //
  1463   // If interpreter_frame is not NULL then it will be filled in.
  1464   // It's size is determined by a previous call to this method,
  1465   // so it should be correct.
  1466   //
  1467   // Note that tempcount is the current size of the expression
  1468   // stack.  For top most frames we will allocate a full sized
  1469   // expression stack and not the trimmed version that non-top
  1470   // frames have.
  1472   int header_words        = InterpreterFrame::header_words;
  1473   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
  1474   int stack_words         = is_top_frame ? method->max_stack() : tempcount;
  1475   int callee_extra_locals = callee_locals - callee_param_count;
  1477   if (interpreter_frame) {
  1478     intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
  1479     interpreterState istate = interpreter_frame->get_interpreterState();
  1480     intptr_t *monitor_base  = (intptr_t*) istate;
  1481     intptr_t *stack_base    = monitor_base - monitor_words;
  1482     intptr_t *stack         = stack_base - tempcount - 1;
  1484     BytecodeInterpreter::layout_interpreterState(istate,
  1485                                                  caller,
  1486                                                  NULL,
  1487                                                  method,
  1488                                                  locals,
  1489                                                  stack,
  1490                                                  stack_base,
  1491                                                  monitor_base,
  1492                                                  NULL,
  1493                                                  is_top_frame);
  1495   return header_words + monitor_words + stack_words + callee_extra_locals;
  1498 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
  1499                                                   frame*    caller,
  1500                                                   frame*    current,
  1501                                                   methodOop method,
  1502                                                   intptr_t* locals,
  1503                                                   intptr_t* stack,
  1504                                                   intptr_t* stack_base,
  1505                                                   intptr_t* monitor_base,
  1506                                                   intptr_t* frame_bottom,
  1507                                                   bool      is_top_frame) {
  1508   istate->set_locals(locals);
  1509   istate->set_method(method);
  1510   istate->set_self_link(istate);
  1511   istate->set_prev_link(NULL);
  1512   // thread will be set by a hacky repurposing of frame::patch_pc()
  1513   // bcp will be set by vframeArrayElement::unpack_on_stack()
  1514   istate->set_constants(method->constants()->cache());
  1515   istate->set_msg(BytecodeInterpreter::method_resume);
  1516   istate->set_bcp_advance(0);
  1517   istate->set_oop_temp(NULL);
  1518   istate->set_mdx(NULL);
  1519   if (caller->is_interpreted_frame()) {
  1520     interpreterState prev = caller->get_interpreterState();
  1521     prev->set_callee(method);
  1522     if (*prev->bcp() == Bytecodes::_invokeinterface)
  1523       prev->set_bcp_advance(5);
  1524     else
  1525       prev->set_bcp_advance(3);
  1527   istate->set_callee(NULL);
  1528   istate->set_monitor_base((BasicObjectLock *) monitor_base);
  1529   istate->set_stack_base(stack_base);
  1530   istate->set_stack(stack);
  1531   istate->set_stack_limit(stack_base - method->max_stack() - 1);
  1534 address CppInterpreter::return_entry(TosState state, int length) {
  1535   ShouldNotCallThis();
  1538 address CppInterpreter::deopt_entry(TosState state, int length) {
  1539   return NULL;
  1542 // Helper for (runtime) stack overflow checks
  1544 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
  1545   return 0;
  1548 // Helper for figuring out if frames are interpreter frames
  1550 bool CppInterpreter::contains(address pc) {
  1551 #ifdef PRODUCT
  1552   ShouldNotCallThis();
  1553 #else
  1554   return false; // make frame::print_value_on work
  1555 #endif // !PRODUCT
  1558 // Result handlers and convertors
  1560 address CppInterpreterGenerator::generate_result_handler_for(
  1561     BasicType type) {
  1562   assembler()->advance(1);
  1563   return ShouldNotCallThisStub();
  1566 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
  1567     BasicType type) {
  1568   assembler()->advance(1);
  1569   return ShouldNotCallThisStub();
  1572 address CppInterpreterGenerator::generate_stack_to_stack_converter(
  1573     BasicType type) {
  1574   assembler()->advance(1);
  1575   return ShouldNotCallThisStub();
  1578 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
  1579     BasicType type) {
  1580   assembler()->advance(1);
  1581   return ShouldNotCallThisStub();
  1584 #endif // CC_INTERP

mercurial