src/cpu/zero/vm/cppInterpreter_zero.cpp

Fri, 15 Jan 2016 22:33:15 +0000

author
kevinw
date
Fri, 15 Jan 2016 22:33:15 +0000
changeset 8368
32b682649973
parent 7675
6d13c17668d1
child 8402
cc78c97abff8
permissions
-rw-r--r--

8132051: Better byte behavior
Reviewed-by: coleenp, roland

     1 /*
     2  * Copyright (c) 2003, 2016, 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/methodData.hpp"
    35 #include "oops/method.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/orderAccess.inline.hpp"
    44 #include "runtime/sharedRuntime.hpp"
    45 #include "runtime/stubRoutines.hpp"
    46 #include "runtime/synchronizer.hpp"
    47 #include "runtime/timer.hpp"
    48 #include "runtime/vframeArray.hpp"
    49 #include "stack_zero.inline.hpp"
    50 #include "utilities/debug.hpp"
    51 #include "utilities/macros.hpp"
    52 #ifdef SHARK
    53 #include "shark/shark_globals.hpp"
    54 #endif
    56 #ifdef CC_INTERP
    58 #define fixup_after_potential_safepoint()       \
    59   method = istate->method()
    61 #define CALL_VM_NOCHECK_NOFIX(func)             \
    62   thread->set_last_Java_frame();                \
    63   func;                                         \
    64   thread->reset_last_Java_frame();
    66 #define CALL_VM_NOCHECK(func)                   \
    67   CALL_VM_NOCHECK_NOFIX(func)                   \
    68   fixup_after_potential_safepoint()
    70 int CppInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
    71   JavaThread *thread = (JavaThread *) THREAD;
    73   // Allocate and initialize our frame.
    74   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
    75   thread->push_zero_frame(frame);
    77   // Execute those bytecodes!
    78   main_loop(0, THREAD);
    80   // No deoptimized frames on the stack
    81   return 0;
    82 }
    84 intptr_t narrow(BasicType type, intptr_t result) {
    85   // mask integer result to narrower return type.
    86   switch (type) {
    87     case T_BOOLEAN:
    88       return result&1;
    89     case T_BYTE:
    90       return (intptr_t)(jbyte)result;
    91     case T_CHAR:
    92       return (intptr_t)(uintptr_t)(jchar)result;
    93     case T_SHORT:
    94       return (intptr_t)(jshort)result;
    95     case T_OBJECT:  // nothing to do fall through
    96     case T_LONG:
    97     case T_INT:
    98     case T_FLOAT:
    99     case T_DOUBLE:
   100     case T_VOID:
   101       return result;
   102     default  : ShouldNotReachHere();
   103   }
   104 }
   107 void CppInterpreter::main_loop(int recurse, TRAPS) {
   108   JavaThread *thread = (JavaThread *) THREAD;
   109   ZeroStack *stack = thread->zero_stack();
   111   // If we are entering from a deopt we may need to call
   112   // ourself a few times in order to get to our frame.
   113   if (recurse)
   114     main_loop(recurse - 1, THREAD);
   116   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
   117   interpreterState istate = frame->interpreter_state();
   118   Method* method = istate->method();
   120   intptr_t *result = NULL;
   121   int result_slots = 0;
   123   while (true) {
   124     // We can set up the frame anchor with everything we want at
   125     // this point as we are thread_in_Java and no safepoints can
   126     // occur until we go to vm mode.  We do have to clear flags
   127     // on return from vm but that is it.
   128     thread->set_last_Java_frame();
   130     // Call the interpreter
   131     if (JvmtiExport::can_post_interpreter_events())
   132       BytecodeInterpreter::runWithChecks(istate);
   133     else
   134       BytecodeInterpreter::run(istate);
   135     fixup_after_potential_safepoint();
   137     // Clear the frame anchor
   138     thread->reset_last_Java_frame();
   140     // Examine the message from the interpreter to decide what to do
   141     if (istate->msg() == BytecodeInterpreter::call_method) {
   142       Method* callee = istate->callee();
   144       // Trim back the stack to put the parameters at the top
   145       stack->set_sp(istate->stack() + 1);
   147       // Make the call
   148       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
   149       fixup_after_potential_safepoint();
   151       // Convert the result
   152       istate->set_stack(stack->sp() - 1);
   154       // Restore the stack
   155       stack->set_sp(istate->stack_limit() + 1);
   157       // Resume the interpreter
   158       istate->set_msg(BytecodeInterpreter::method_resume);
   159     }
   160     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
   161       int monitor_words = frame::interpreter_frame_monitor_size();
   163       // Allocate the space
   164       stack->overflow_check(monitor_words, THREAD);
   165       if (HAS_PENDING_EXCEPTION)
   166         break;
   167       stack->alloc(monitor_words * wordSize);
   169       // Move the expression stack contents
   170       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
   171         *(p - monitor_words) = *p;
   173       // Move the expression stack pointers
   174       istate->set_stack_limit(istate->stack_limit() - monitor_words);
   175       istate->set_stack(istate->stack() - monitor_words);
   176       istate->set_stack_base(istate->stack_base() - monitor_words);
   178       // Zero the new monitor so the interpreter can find it.
   179       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
   181       // Resume the interpreter
   182       istate->set_msg(BytecodeInterpreter::got_monitors);
   183     }
   184     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
   185       // Copy the result into the caller's frame
   186       result_slots = type2size[result_type_of(method)];
   187       assert(result_slots >= 0 && result_slots <= 2, "what?");
   188       result = istate->stack() + result_slots;
   189       break;
   190     }
   191     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
   192       assert(HAS_PENDING_EXCEPTION, "should do");
   193       break;
   194     }
   195     else if (istate->msg() == BytecodeInterpreter::do_osr) {
   196       // Unwind the current frame
   197       thread->pop_zero_frame();
   199       // Remove any extension of the previous frame
   200       int extra_locals = method->max_locals() - method->size_of_parameters();
   201       stack->set_sp(stack->sp() + extra_locals);
   203       // Jump into the OSR method
   204       Interpreter::invoke_osr(
   205         method, istate->osr_entry(), istate->osr_buf(), THREAD);
   206       return;
   207     }
   208     else {
   209       ShouldNotReachHere();
   210     }
   211   }
   213   // Unwind the current frame
   214   thread->pop_zero_frame();
   216   // Pop our local variables
   217   stack->set_sp(stack->sp() + method->max_locals());
   219   // Push our result
   220   for (int i = 0; i < result_slots; i++) {
   221     // Adjust result to smaller
   222     intptr_t res = result[-i];
   223     if (result_slots == 1) {
   224       res = narrow(result_type_of(method), res);
   225     }
   226     stack->push(res);
   227   }
   228 }
   230 int CppInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
   231   // Make sure method is native and not abstract
   232   assert(method->is_native() && !method->is_abstract(), "should be");
   234   JavaThread *thread = (JavaThread *) THREAD;
   235   ZeroStack *stack = thread->zero_stack();
   237   // Allocate and initialize our frame
   238   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
   239   thread->push_zero_frame(frame);
   240   interpreterState istate = frame->interpreter_state();
   241   intptr_t *locals = istate->locals();
   243   // Update the invocation counter
   244   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
   245     MethodCounters* mcs = method->method_counters();
   246     if (mcs == NULL) {
   247       CALL_VM_NOCHECK(mcs = InterpreterRuntime::build_method_counters(thread, method));
   248       if (HAS_PENDING_EXCEPTION)
   249         goto unwind_and_return;
   250     }
   251     InvocationCounter *counter = mcs->invocation_counter();
   252     counter->increment();
   253     if (counter->reached_InvocationLimit(mcs->backedge_counter())) {
   254       CALL_VM_NOCHECK(
   255         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
   256       if (HAS_PENDING_EXCEPTION)
   257         goto unwind_and_return;
   258     }
   259   }
   261   // Lock if necessary
   262   BasicObjectLock *monitor;
   263   monitor = NULL;
   264   if (method->is_synchronized()) {
   265     monitor = (BasicObjectLock*) istate->stack_base();
   266     oop lockee = monitor->obj();
   267     markOop disp = lockee->mark()->set_unlocked();
   269     monitor->lock()->set_displaced_header(disp);
   270     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
   271       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
   272         monitor->lock()->set_displaced_header(NULL);
   273       }
   274       else {
   275         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
   276         if (HAS_PENDING_EXCEPTION)
   277           goto unwind_and_return;
   278       }
   279     }
   280   }
   282   // Get the signature handler
   283   InterpreterRuntime::SignatureHandler *handler; {
   284     address handlerAddr = method->signature_handler();
   285     if (handlerAddr == NULL) {
   286       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
   287       if (HAS_PENDING_EXCEPTION)
   288         goto unlock_unwind_and_return;
   290       handlerAddr = method->signature_handler();
   291       assert(handlerAddr != NULL, "eh?");
   292     }
   293     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
   294       CALL_VM_NOCHECK(handlerAddr =
   295         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
   296       if (HAS_PENDING_EXCEPTION)
   297         goto unlock_unwind_and_return;
   298     }
   299     handler = \
   300       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
   301   }
   303   // Get the native function entry point
   304   address function;
   305   function = method->native_function();
   306   assert(function != NULL, "should be set if signature handler is");
   308   // Build the argument list
   309   stack->overflow_check(handler->argument_count() * 2, THREAD);
   310   if (HAS_PENDING_EXCEPTION)
   311     goto unlock_unwind_and_return;
   313   void **arguments;
   314   void *mirror; {
   315     arguments =
   316       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
   317     void **dst = arguments;
   319     void *env = thread->jni_environment();
   320     *(dst++) = &env;
   322     if (method->is_static()) {
   323       istate->set_oop_temp(
   324         method->constants()->pool_holder()->java_mirror());
   325       mirror = istate->oop_temp_addr();
   326       *(dst++) = &mirror;
   327     }
   329     intptr_t *src = locals;
   330     for (int i = dst - arguments; i < handler->argument_count(); i++) {
   331       ffi_type *type = handler->argument_type(i);
   332       if (type == &ffi_type_pointer) {
   333         if (*src) {
   334           stack->push((intptr_t) src);
   335           *(dst++) = stack->sp();
   336         }
   337         else {
   338           *(dst++) = src;
   339         }
   340         src--;
   341       }
   342       else if (type->size == 4) {
   343         *(dst++) = src--;
   344       }
   345       else if (type->size == 8) {
   346         src--;
   347         *(dst++) = src--;
   348       }
   349       else {
   350         ShouldNotReachHere();
   351       }
   352     }
   353   }
   355   // Set up the Java frame anchor
   356   thread->set_last_Java_frame();
   358   // Change the thread state to _thread_in_native
   359   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
   361   // Make the call
   362   intptr_t result[4 - LogBytesPerWord];
   363   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
   365   // Change the thread state back to _thread_in_Java.
   366   // ThreadStateTransition::transition_from_native() cannot be used
   367   // here because it does not check for asynchronous exceptions.
   368   // We have to manage the transition ourself.
   369   thread->set_thread_state(_thread_in_native_trans);
   371   // Make sure new state is visible in the GC thread
   372   if (os::is_MP()) {
   373     if (UseMembar) {
   374       OrderAccess::fence();
   375     }
   376     else {
   377       InterfaceSupport::serialize_memory(thread);
   378     }
   379   }
   381   // Handle safepoint operations, pending suspend requests,
   382   // and pending asynchronous exceptions.
   383   if (SafepointSynchronize::do_call_back() ||
   384       thread->has_special_condition_for_native_trans()) {
   385     JavaThread::check_special_condition_for_native_trans(thread);
   386     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
   387   }
   389   // Finally we can change the thread state to _thread_in_Java.
   390   thread->set_thread_state(_thread_in_Java);
   391   fixup_after_potential_safepoint();
   393   // Clear the frame anchor
   394   thread->reset_last_Java_frame();
   396   // If the result was an oop then unbox it and store it in
   397   // oop_temp where the garbage collector can see it before
   398   // we release the handle it might be protected by.
   399   if (handler->result_type() == &ffi_type_pointer) {
   400     if (result[0])
   401       istate->set_oop_temp(*(oop *) result[0]);
   402     else
   403       istate->set_oop_temp(NULL);
   404   }
   406   // Reset handle block
   407   thread->active_handles()->clear();
   409  unlock_unwind_and_return:
   411   // Unlock if necessary
   412   if (monitor) {
   413     BasicLock *lock = monitor->lock();
   414     markOop header = lock->displaced_header();
   415     oop rcvr = monitor->obj();
   416     monitor->set_obj(NULL);
   418     if (header != NULL) {
   419       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
   420         monitor->set_obj(rcvr); {
   421           HandleMark hm(thread);
   422           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
   423         }
   424       }
   425     }
   426   }
   428  unwind_and_return:
   430   // Unwind the current activation
   431   thread->pop_zero_frame();
   433   // Pop our parameters
   434   stack->set_sp(stack->sp() + method->size_of_parameters());
   436   // Push our result
   437   if (!HAS_PENDING_EXCEPTION) {
   438     BasicType type = result_type_of(method);
   439     stack->set_sp(stack->sp() - type2size[type]);
   441     switch (type) {
   442     case T_VOID:
   443       break;
   445     case T_BOOLEAN:
   446 #ifndef VM_LITTLE_ENDIAN
   447       result[0] <<= (BitsPerWord - BitsPerByte);
   448 #endif
   449       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
   450       break;
   452     case T_CHAR:
   453 #ifndef VM_LITTLE_ENDIAN
   454       result[0] <<= (BitsPerWord - BitsPerShort);
   455 #endif
   456       SET_LOCALS_INT(*(jchar *) result, 0);
   457       break;
   459     case T_BYTE:
   460 #ifndef VM_LITTLE_ENDIAN
   461       result[0] <<= (BitsPerWord - BitsPerByte);
   462 #endif
   463       SET_LOCALS_INT(*(jbyte *) result, 0);
   464       break;
   466     case T_SHORT:
   467 #ifndef VM_LITTLE_ENDIAN
   468       result[0] <<= (BitsPerWord - BitsPerShort);
   469 #endif
   470       SET_LOCALS_INT(*(jshort *) result, 0);
   471       break;
   473     case T_INT:
   474 #ifndef VM_LITTLE_ENDIAN
   475       result[0] <<= (BitsPerWord - BitsPerInt);
   476 #endif
   477       SET_LOCALS_INT(*(jint *) result, 0);
   478       break;
   480     case T_LONG:
   481       SET_LOCALS_LONG(*(jlong *) result, 0);
   482       break;
   484     case T_FLOAT:
   485       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
   486       break;
   488     case T_DOUBLE:
   489       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
   490       break;
   492     case T_OBJECT:
   493     case T_ARRAY:
   494       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
   495       break;
   497     default:
   498       ShouldNotReachHere();
   499     }
   500   }
   502   // No deoptimized frames on the stack
   503   return 0;
   504 }
   506 int CppInterpreter::accessor_entry(Method* method, intptr_t UNUSED, TRAPS) {
   507   JavaThread *thread = (JavaThread *) THREAD;
   508   ZeroStack *stack = thread->zero_stack();
   509   intptr_t *locals = stack->sp();
   511   // Drop into the slow path if we need a safepoint check
   512   if (SafepointSynchronize::do_call_back()) {
   513     return normal_entry(method, 0, THREAD);
   514   }
   516   // Load the object pointer and drop into the slow path
   517   // if we have a NullPointerException
   518   oop object = LOCALS_OBJECT(0);
   519   if (object == NULL) {
   520     return normal_entry(method, 0, THREAD);
   521   }
   523   // Read the field index from the bytecode, which looks like this:
   524   //  0:  aload_0
   525   //  1:  getfield
   526   //  2:    index
   527   //  3:    index
   528   //  4:  ireturn/areturn
   529   // NB this is not raw bytecode: index is in machine order
   530   u1 *code = method->code_base();
   531   assert(code[0] == Bytecodes::_aload_0 &&
   532          code[1] == Bytecodes::_getfield &&
   533          (code[4] == Bytecodes::_ireturn ||
   534           code[4] == Bytecodes::_areturn), "should do");
   535   u2 index = Bytes::get_native_u2(&code[2]);
   537   // Get the entry from the constant pool cache, and drop into
   538   // the slow path if it has not been resolved
   539   ConstantPoolCache* cache = method->constants()->cache();
   540   ConstantPoolCacheEntry* entry = cache->entry_at(index);
   541   if (!entry->is_resolved(Bytecodes::_getfield)) {
   542     return normal_entry(method, 0, THREAD);
   543   }
   545   // Get the result and push it onto the stack
   546   switch (entry->flag_state()) {
   547   case ltos:
   548   case dtos:
   549     stack->overflow_check(1, CHECK_0);
   550     stack->alloc(wordSize);
   551     break;
   552   }
   553   if (entry->is_volatile()) {
   554     switch (entry->flag_state()) {
   555     case ctos:
   556       SET_LOCALS_INT(object->char_field_acquire(entry->f2_as_index()), 0);
   557       break;
   559     case btos:
   560     case ztos:
   561       SET_LOCALS_INT(object->byte_field_acquire(entry->f2_as_index()), 0);
   562       break;
   564     case stos:
   565       SET_LOCALS_INT(object->short_field_acquire(entry->f2_as_index()), 0);
   566       break;
   568     case itos:
   569       SET_LOCALS_INT(object->int_field_acquire(entry->f2_as_index()), 0);
   570       break;
   572     case ltos:
   573       SET_LOCALS_LONG(object->long_field_acquire(entry->f2_as_index()), 0);
   574       break;
   576     case ftos:
   577       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2_as_index()), 0);
   578       break;
   580     case dtos:
   581       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2_as_index()), 0);
   582       break;
   584     case atos:
   585       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2_as_index()), 0);
   586       break;
   588     default:
   589       ShouldNotReachHere();
   590     }
   591   }
   592   else {
   593     switch (entry->flag_state()) {
   594     case ctos:
   595       SET_LOCALS_INT(object->char_field(entry->f2_as_index()), 0);
   596       break;
   598     case btos:
   599     case ztos:
   600       SET_LOCALS_INT(object->byte_field(entry->f2_as_index()), 0);
   601       break;
   603     case stos:
   604       SET_LOCALS_INT(object->short_field(entry->f2_as_index()), 0);
   605       break;
   607     case itos:
   608       SET_LOCALS_INT(object->int_field(entry->f2_as_index()), 0);
   609       break;
   611     case ltos:
   612       SET_LOCALS_LONG(object->long_field(entry->f2_as_index()), 0);
   613       break;
   615     case ftos:
   616       SET_LOCALS_FLOAT(object->float_field(entry->f2_as_index()), 0);
   617       break;
   619     case dtos:
   620       SET_LOCALS_DOUBLE(object->double_field(entry->f2_as_index()), 0);
   621       break;
   623     case atos:
   624       SET_LOCALS_OBJECT(object->obj_field(entry->f2_as_index()), 0);
   625       break;
   627     default:
   628       ShouldNotReachHere();
   629     }
   630   }
   632   // No deoptimized frames on the stack
   633   return 0;
   634 }
   636 int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
   637   JavaThread *thread = (JavaThread *) THREAD;
   638   ZeroStack *stack = thread->zero_stack();
   640   // Drop into the slow path if we need a safepoint check
   641   if (SafepointSynchronize::do_call_back()) {
   642     return normal_entry(method, 0, THREAD);
   643   }
   645   // Pop our parameters
   646   stack->set_sp(stack->sp() + method->size_of_parameters());
   648   // No deoptimized frames on the stack
   649   return 0;
   650 }
   652 // The new slots will be inserted before slot insert_before.
   653 // Slots < insert_before will have the same slot number after the insert.
   654 // Slots >= insert_before will become old_slot + num_slots.
   655 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
   656   JavaThread *thread = (JavaThread *) THREAD;
   657   ZeroStack *stack = thread->zero_stack();
   659   // Allocate the space
   660   stack->overflow_check(num_slots, CHECK);
   661   stack->alloc(num_slots * wordSize);
   662   intptr_t *vmslots = stack->sp();
   664   // Shuffle everything up
   665   for (int i = 0; i < insert_before; i++)
   666     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
   667 }
   669 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
   670   JavaThread *thread = (JavaThread *) THREAD;
   671   ZeroStack *stack = thread->zero_stack();
   672   intptr_t *vmslots = stack->sp();
   674   // Move everything down
   675   for (int i = first_slot - 1; i >= 0; i--)
   676     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
   678   // Deallocate the space
   679   stack->set_sp(stack->sp() + num_slots);
   680 }
   682 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
   683   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
   684   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
   685   return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
   686 }
   688 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
   689                                               oop method_handle) {
   690   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
   691   int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
   693   return stack->sp() + argument_slots;
   694 }
   696 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
   697                                                 Symbol*     name,
   698                                                 char*       message))
   699   THROW_MSG(name, message);
   700 IRT_END
   702 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
   703   JavaThread *thread = (JavaThread *) THREAD;
   704   ZeroStack *stack = thread->zero_stack();
   706   // Calculate the size of the frame we'll build, including
   707   // any adjustments to the caller's frame that we'll make.
   708   int extra_locals  = 0;
   709   int monitor_words = 0;
   710   int stack_words   = 0;
   712   if (!method->is_native()) {
   713     extra_locals = method->max_locals() - method->size_of_parameters();
   714     stack_words  = method->max_stack();
   715   }
   716   if (method->is_synchronized()) {
   717     monitor_words = frame::interpreter_frame_monitor_size();
   718   }
   719   stack->overflow_check(
   720     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
   722   // Adjust the caller's stack frame to accomodate any additional
   723   // local variables we have contiguously with our parameters.
   724   for (int i = 0; i < extra_locals; i++)
   725     stack->push(0);
   727   intptr_t *locals;
   728   if (method->is_native())
   729     locals = stack->sp() + (method->size_of_parameters() - 1);
   730   else
   731     locals = stack->sp() + (method->max_locals() - 1);
   733   stack->push(0); // next_frame, filled in later
   734   intptr_t *fp = stack->sp();
   735   assert(fp - stack->sp() == next_frame_off, "should be");
   737   stack->push(INTERPRETER_FRAME);
   738   assert(fp - stack->sp() == frame_type_off, "should be");
   740   interpreterState istate =
   741     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
   742   assert(fp - stack->sp() == istate_off, "should be");
   744   istate->set_locals(locals);
   745   istate->set_method(method);
   746   istate->set_self_link(istate);
   747   istate->set_prev_link(NULL);
   748   istate->set_thread(thread);
   749   istate->set_bcp(method->is_native() ? NULL : method->code_base());
   750   istate->set_constants(method->constants()->cache());
   751   istate->set_msg(BytecodeInterpreter::method_entry);
   752   istate->set_oop_temp(NULL);
   753   istate->set_mdx(NULL);
   754   istate->set_callee(NULL);
   756   istate->set_monitor_base((BasicObjectLock *) stack->sp());
   757   if (method->is_synchronized()) {
   758     BasicObjectLock *monitor =
   759       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
   760     oop object;
   761     if (method->is_static())
   762       object = method->constants()->pool_holder()->java_mirror();
   763     else
   764       object = (oop) (void*)locals[0];
   765     monitor->set_obj(object);
   766   }
   768   istate->set_stack_base(stack->sp());
   769   istate->set_stack(stack->sp() - 1);
   770   if (stack_words)
   771     stack->alloc(stack_words * wordSize);
   772   istate->set_stack_limit(stack->sp() - 1);
   774   return (InterpreterFrame *) fp;
   775 }
   777 int AbstractInterpreter::BasicType_as_index(BasicType type) {
   778   int i = 0;
   779   switch (type) {
   780     case T_BOOLEAN: i = 0; break;
   781     case T_CHAR   : i = 1; break;
   782     case T_BYTE   : i = 2; break;
   783     case T_SHORT  : i = 3; break;
   784     case T_INT    : i = 4; break;
   785     case T_LONG   : i = 5; break;
   786     case T_VOID   : i = 6; break;
   787     case T_FLOAT  : i = 7; break;
   788     case T_DOUBLE : i = 8; break;
   789     case T_OBJECT : i = 9; break;
   790     case T_ARRAY  : i = 9; break;
   791     default       : ShouldNotReachHere();
   792   }
   793   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
   794          "index out of bounds");
   795   return i;
   796 }
   798 BasicType CppInterpreter::result_type_of(Method* method) {
   799   BasicType t;
   800   switch (method->result_index()) {
   801     case 0 : t = T_BOOLEAN; break;
   802     case 1 : t = T_CHAR;    break;
   803     case 2 : t = T_BYTE;    break;
   804     case 3 : t = T_SHORT;   break;
   805     case 4 : t = T_INT;     break;
   806     case 5 : t = T_LONG;    break;
   807     case 6 : t = T_VOID;    break;
   808     case 7 : t = T_FLOAT;   break;
   809     case 8 : t = T_DOUBLE;  break;
   810     case 9 : t = T_OBJECT;  break;
   811     default: ShouldNotReachHere();
   812   }
   813   assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
   814          "out of step with AbstractInterpreter::BasicType_as_index");
   815   return t;
   816 }
   818 address InterpreterGenerator::generate_empty_entry() {
   819   if (!UseFastEmptyMethods)
   820     return NULL;
   822   return generate_entry((address) CppInterpreter::empty_entry);
   823 }
   825 address InterpreterGenerator::generate_accessor_entry() {
   826   if (!UseFastAccessorMethods)
   827     return NULL;
   829   return generate_entry((address) CppInterpreter::accessor_entry);
   830 }
   832 address InterpreterGenerator::generate_Reference_get_entry(void) {
   833 #if INCLUDE_ALL_GCS
   834   if (UseG1GC) {
   835     // We need to generate have a routine that generates code to:
   836     //   * load the value in the referent field
   837     //   * passes that value to the pre-barrier.
   838     //
   839     // In the case of G1 this will record the value of the
   840     // referent in an SATB buffer if marking is active.
   841     // This will cause concurrent marking to mark the referent
   842     // field as live.
   843     Unimplemented();
   844   }
   845 #endif // INCLUDE_ALL_GCS
   847   // If G1 is not enabled then attempt to go through the accessor entry point
   848   // Reference.get is an accessor
   849   return generate_accessor_entry();
   850 }
   852 address InterpreterGenerator::generate_native_entry(bool synchronized) {
   853   assert(synchronized == false, "should be");
   855   return generate_entry((address) CppInterpreter::native_entry);
   856 }
   858 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
   859   assert(synchronized == false, "should be");
   861   return generate_entry((address) CppInterpreter::normal_entry);
   862 }
   864 address AbstractInterpreterGenerator::generate_method_entry(
   865     AbstractInterpreter::MethodKind kind) {
   866   address entry_point = NULL;
   868   switch (kind) {
   869   case Interpreter::zerolocals:
   870   case Interpreter::zerolocals_synchronized:
   871     break;
   873   case Interpreter::native:
   874     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
   875     break;
   877   case Interpreter::native_synchronized:
   878     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
   879     break;
   881   case Interpreter::empty:
   882     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
   883     break;
   885   case Interpreter::accessor:
   886     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
   887     break;
   889   case Interpreter::abstract:
   890     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
   891     break;
   893   case Interpreter::java_lang_math_sin:
   894   case Interpreter::java_lang_math_cos:
   895   case Interpreter::java_lang_math_tan:
   896   case Interpreter::java_lang_math_abs:
   897   case Interpreter::java_lang_math_log:
   898   case Interpreter::java_lang_math_log10:
   899   case Interpreter::java_lang_math_sqrt:
   900   case Interpreter::java_lang_math_pow:
   901   case Interpreter::java_lang_math_exp:
   902     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
   903     break;
   905   case Interpreter::java_lang_ref_reference_get:
   906     entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
   907     break;
   909   default:
   910     ShouldNotReachHere();
   911   }
   913   if (entry_point == NULL)
   914     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
   916   return entry_point;
   917 }
   919 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
   920  : CppInterpreterGenerator(code) {
   921    generate_all();
   922 }
   924 // Deoptimization helpers
   926 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
   927   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
   929   int size_in_words = size >> LogBytesPerWord;
   930   assert(size_in_words * wordSize == size, "unaligned");
   931   assert(size_in_words >= header_words, "too small");
   932   stack->overflow_check(size_in_words, CHECK_NULL);
   934   stack->push(0); // next_frame, filled in later
   935   intptr_t *fp = stack->sp();
   936   assert(fp - stack->sp() == next_frame_off, "should be");
   938   stack->push(INTERPRETER_FRAME);
   939   assert(fp - stack->sp() == frame_type_off, "should be");
   941   interpreterState istate =
   942     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
   943   assert(fp - stack->sp() == istate_off, "should be");
   944   istate->set_self_link(NULL); // mark invalid
   946   stack->alloc((size_in_words - header_words) * wordSize);
   948   return (InterpreterFrame *) fp;
   949 }
   951 int AbstractInterpreter::size_activation(int       max_stack,
   952                                          int       tempcount,
   953                                          int       extra_args,
   954                                          int       moncount,
   955                                          int       callee_param_count,
   956                                          int       callee_locals,
   957                                          bool      is_top_frame) {
   958   int header_words        = InterpreterFrame::header_words;
   959   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
   960   int stack_words         = is_top_frame ? max_stack : tempcount;
   961   int callee_extra_locals = callee_locals - callee_param_count;
   963   return header_words + monitor_words + stack_words + callee_extra_locals;
   964 }
   966 void AbstractInterpreter::layout_activation(Method* method,
   967                                             int       tempcount,
   968                                             int       popframe_extra_args,
   969                                             int       moncount,
   970                                             int       caller_actual_parameters,
   971                                             int       callee_param_count,
   972                                             int       callee_locals,
   973                                             frame*    caller,
   974                                             frame*    interpreter_frame,
   975                                             bool      is_top_frame,
   976                                             bool      is_bottom_frame) {
   977   assert(popframe_extra_args == 0, "what to do?");
   978   assert(!is_top_frame || (!callee_locals && !callee_param_count),
   979          "top frame should have no caller");
   981   // This code must exactly match what InterpreterFrame::build
   982   // does (the full InterpreterFrame::build, that is, not the
   983   // one that creates empty frames for the deoptimizer).
   984   //
   985   // interpreter_frame will be filled in.  It's size is determined by
   986   // a previous call to the size_activation() method,
   987   //
   988   // Note that tempcount is the current size of the expression
   989   // stack.  For top most frames we will allocate a full sized
   990   // expression stack and not the trimmed version that non-top
   991   // frames have.
   993   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
   994   intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
   995   interpreterState istate = interpreter_frame->get_interpreterState();
   996   intptr_t *monitor_base  = (intptr_t*) istate;
   997   intptr_t *stack_base    = monitor_base - monitor_words;
   998   intptr_t *stack         = stack_base - tempcount - 1;
  1000   BytecodeInterpreter::layout_interpreterState(istate,
  1001                                                caller,
  1002                                                NULL,
  1003                                                method,
  1004                                                locals,
  1005                                                stack,
  1006                                                stack_base,
  1007                                                monitor_base,
  1008                                                NULL,
  1009                                                is_top_frame);
  1012 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
  1013                                                   frame*    caller,
  1014                                                   frame*    current,
  1015                                                   Method* method,
  1016                                                   intptr_t* locals,
  1017                                                   intptr_t* stack,
  1018                                                   intptr_t* stack_base,
  1019                                                   intptr_t* monitor_base,
  1020                                                   intptr_t* frame_bottom,
  1021                                                   bool      is_top_frame) {
  1022   istate->set_locals(locals);
  1023   istate->set_method(method);
  1024   istate->set_self_link(istate);
  1025   istate->set_prev_link(NULL);
  1026   // thread will be set by a hacky repurposing of frame::patch_pc()
  1027   // bcp will be set by vframeArrayElement::unpack_on_stack()
  1028   istate->set_constants(method->constants()->cache());
  1029   istate->set_msg(BytecodeInterpreter::method_resume);
  1030   istate->set_bcp_advance(0);
  1031   istate->set_oop_temp(NULL);
  1032   istate->set_mdx(NULL);
  1033   if (caller->is_interpreted_frame()) {
  1034     interpreterState prev = caller->get_interpreterState();
  1035     prev->set_callee(method);
  1036     if (*prev->bcp() == Bytecodes::_invokeinterface)
  1037       prev->set_bcp_advance(5);
  1038     else
  1039       prev->set_bcp_advance(3);
  1041   istate->set_callee(NULL);
  1042   istate->set_monitor_base((BasicObjectLock *) monitor_base);
  1043   istate->set_stack_base(stack_base);
  1044   istate->set_stack(stack);
  1045   istate->set_stack_limit(stack_base - method->max_stack() - 1);
  1048 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
  1049   ShouldNotCallThis();
  1050   return NULL;
  1053 address CppInterpreter::deopt_entry(TosState state, int length) {
  1054   return NULL;
  1057 // Helper for (runtime) stack overflow checks
  1059 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
  1060   return 0;
  1063 // Helper for figuring out if frames are interpreter frames
  1065 bool CppInterpreter::contains(address pc) {
  1066   return false; // make frame::print_value_on work
  1069 // Result handlers and convertors
  1071 address CppInterpreterGenerator::generate_result_handler_for(
  1072     BasicType type) {
  1073   assembler()->advance(1);
  1074   return ShouldNotCallThisStub();
  1077 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
  1078     BasicType type) {
  1079   assembler()->advance(1);
  1080   return ShouldNotCallThisStub();
  1083 address CppInterpreterGenerator::generate_stack_to_stack_converter(
  1084     BasicType type) {
  1085   assembler()->advance(1);
  1086   return ShouldNotCallThisStub();
  1089 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
  1090     BasicType type) {
  1091   assembler()->advance(1);
  1092   return ShouldNotCallThisStub();
  1095 #endif // CC_INTERP

mercurial