hotspot/src/cpu/zero/vm/cppInterpreter_zero.cpp
changeset 4013 b154310845de
child 4448 d6ec2737186c
equal deleted inserted replaced
4012:579b7bad9983 4013:b154310845de
       
     1 /*
       
     2  * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * Copyright 2007, 2008, 2009 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    21  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    22  * have any questions.
       
    23  *
       
    24  */
       
    25 
       
    26 #include "incls/_precompiled.incl"
       
    27 #include "incls/_cppInterpreter_zero.cpp.incl"
       
    28 
       
    29 #ifdef CC_INTERP
       
    30 
       
    31 #define fixup_after_potential_safepoint()       \
       
    32   method = istate->method()
       
    33 
       
    34 #define CALL_VM_NOCHECK(func)                   \
       
    35   thread->set_last_Java_frame();                \
       
    36   func;                                         \
       
    37   thread->reset_last_Java_frame();              \
       
    38   fixup_after_potential_safepoint()
       
    39 
       
    40 void CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
       
    41   JavaThread *thread = (JavaThread *) THREAD;
       
    42   ZeroStack *stack = thread->zero_stack();
       
    43 
       
    44   // Adjust the caller's stack frame to accomodate any additional
       
    45   // local variables we have contiguously with our parameters.
       
    46   int extra_locals = method->max_locals() - method->size_of_parameters();
       
    47   if (extra_locals > 0) {
       
    48     if (extra_locals > stack->available_words()) {
       
    49       Unimplemented();
       
    50     }
       
    51     for (int i = 0; i < extra_locals; i++)
       
    52       stack->push(0);
       
    53   }
       
    54 
       
    55   // Allocate and initialize our frame.
       
    56   InterpreterFrame *frame = InterpreterFrame::build(stack, method, thread);
       
    57   thread->push_zero_frame(frame);
       
    58 
       
    59   // Execute those bytecodes!
       
    60   main_loop(0, THREAD);
       
    61 }
       
    62 
       
    63 void CppInterpreter::main_loop(int recurse, TRAPS) {
       
    64   JavaThread *thread = (JavaThread *) THREAD;
       
    65   ZeroStack *stack = thread->zero_stack();
       
    66 
       
    67   // If we are entering from a deopt we may need to call
       
    68   // ourself a few times in order to get to our frame.
       
    69   if (recurse)
       
    70     main_loop(recurse - 1, THREAD);
       
    71 
       
    72   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
       
    73   interpreterState istate = frame->interpreter_state();
       
    74   methodOop method = istate->method();
       
    75 
       
    76   intptr_t *result = NULL;
       
    77   int result_slots = 0;
       
    78 
       
    79   // Check we're not about to run out of stack
       
    80   if (stack_overflow_imminent(thread)) {
       
    81     CALL_VM_NOCHECK(InterpreterRuntime::throw_StackOverflowError(thread));
       
    82     goto unwind_and_return;
       
    83   }
       
    84 
       
    85   while (true) {
       
    86     // We can set up the frame anchor with everything we want at
       
    87     // this point as we are thread_in_Java and no safepoints can
       
    88     // occur until we go to vm mode.  We do have to clear flags
       
    89     // on return from vm but that is it.
       
    90     thread->set_last_Java_frame();
       
    91 
       
    92     // Call the interpreter
       
    93     if (JvmtiExport::can_post_interpreter_events())
       
    94       BytecodeInterpreter::runWithChecks(istate);
       
    95     else
       
    96       BytecodeInterpreter::run(istate);
       
    97     fixup_after_potential_safepoint();
       
    98 
       
    99     // Clear the frame anchor
       
   100     thread->reset_last_Java_frame();
       
   101 
       
   102     // Examine the message from the interpreter to decide what to do
       
   103     if (istate->msg() == BytecodeInterpreter::call_method) {
       
   104       methodOop callee = istate->callee();
       
   105 
       
   106       // Trim back the stack to put the parameters at the top
       
   107       stack->set_sp(istate->stack() + 1);
       
   108 
       
   109       // Make the call
       
   110       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
       
   111       fixup_after_potential_safepoint();
       
   112 
       
   113       // Convert the result
       
   114       istate->set_stack(stack->sp() - 1);
       
   115 
       
   116       // Restore the stack
       
   117       stack->set_sp(istate->stack_limit() + 1);
       
   118 
       
   119       // Resume the interpreter
       
   120       istate->set_msg(BytecodeInterpreter::method_resume);
       
   121     }
       
   122     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
       
   123       int monitor_words = frame::interpreter_frame_monitor_size();
       
   124 
       
   125       // Allocate the space
       
   126       if (monitor_words > stack->available_words()) {
       
   127         Unimplemented();
       
   128       }
       
   129       stack->alloc(monitor_words * wordSize);
       
   130 
       
   131       // Move the expression stack contents
       
   132       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
       
   133         *(p - monitor_words) = *p;
       
   134 
       
   135       // Move the expression stack pointers
       
   136       istate->set_stack_limit(istate->stack_limit() - monitor_words);
       
   137       istate->set_stack(istate->stack() - monitor_words);
       
   138       istate->set_stack_base(istate->stack_base() - monitor_words);
       
   139 
       
   140       // Zero the new monitor so the interpreter can find it.
       
   141       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
       
   142 
       
   143       // Resume the interpreter
       
   144       istate->set_msg(BytecodeInterpreter::got_monitors);
       
   145     }
       
   146     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
       
   147       // Copy the result into the caller's frame
       
   148       result_slots = type2size[method->result_type()];
       
   149       assert(result_slots >= 0 && result_slots <= 2, "what?");
       
   150       result = istate->stack() + result_slots;
       
   151       break;
       
   152     }
       
   153     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
       
   154       assert(HAS_PENDING_EXCEPTION, "should do");
       
   155       break;
       
   156     }
       
   157     else if (istate->msg() == BytecodeInterpreter::do_osr) {
       
   158       // Unwind the current frame
       
   159       thread->pop_zero_frame();
       
   160 
       
   161       // Remove any extension of the previous frame
       
   162       int extra_locals = method->max_locals() - method->size_of_parameters();
       
   163       stack->set_sp(stack->sp() + extra_locals);
       
   164 
       
   165       // Jump into the OSR method
       
   166       Interpreter::invoke_osr(
       
   167         method, istate->osr_entry(), istate->osr_buf(), THREAD);
       
   168       return;
       
   169     }
       
   170     else {
       
   171       ShouldNotReachHere();
       
   172     }
       
   173   }
       
   174 
       
   175  unwind_and_return:
       
   176 
       
   177   // Unwind the current frame
       
   178   thread->pop_zero_frame();
       
   179 
       
   180   // Pop our local variables
       
   181   stack->set_sp(stack->sp() + method->max_locals());
       
   182 
       
   183   // Push our result
       
   184   for (int i = 0; i < result_slots; i++)
       
   185     stack->push(result[-i]);
       
   186 }
       
   187 
       
   188 void CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
       
   189   // Make sure method is native and not abstract
       
   190   assert(method->is_native() && !method->is_abstract(), "should be");
       
   191 
       
   192   JavaThread *thread = (JavaThread *) THREAD;
       
   193   ZeroStack *stack = thread->zero_stack();
       
   194 
       
   195   // Allocate and initialize our frame
       
   196   InterpreterFrame *frame = InterpreterFrame::build(stack, method, thread);
       
   197   thread->push_zero_frame(frame);
       
   198   interpreterState istate = frame->interpreter_state();
       
   199   intptr_t *locals = istate->locals();
       
   200 
       
   201   // Check we're not about to run out of stack
       
   202   if (stack_overflow_imminent(thread)) {
       
   203     CALL_VM_NOCHECK(InterpreterRuntime::throw_StackOverflowError(thread));
       
   204     goto unwind_and_return;
       
   205   }
       
   206 
       
   207   // Lock if necessary
       
   208   BasicObjectLock *monitor;
       
   209   monitor = NULL;
       
   210   if (method->is_synchronized()) {
       
   211     monitor = (BasicObjectLock*) istate->stack_base();
       
   212     oop lockee = monitor->obj();
       
   213     markOop disp = lockee->mark()->set_unlocked();
       
   214 
       
   215     monitor->lock()->set_displaced_header(disp);
       
   216     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
       
   217       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
       
   218         monitor->lock()->set_displaced_header(NULL);
       
   219       }
       
   220       else {
       
   221         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
       
   222         if (HAS_PENDING_EXCEPTION)
       
   223           goto unwind_and_return;
       
   224       }
       
   225     }
       
   226   }
       
   227 
       
   228   // Get the signature handler
       
   229   InterpreterRuntime::SignatureHandler *handler; {
       
   230     address handlerAddr = method->signature_handler();
       
   231     if (handlerAddr == NULL) {
       
   232       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
       
   233       if (HAS_PENDING_EXCEPTION)
       
   234         goto unwind_and_return;
       
   235 
       
   236       handlerAddr = method->signature_handler();
       
   237       assert(handlerAddr != NULL, "eh?");
       
   238     }
       
   239     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
       
   240       CALL_VM_NOCHECK(handlerAddr =
       
   241         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
       
   242       if (HAS_PENDING_EXCEPTION)
       
   243         goto unwind_and_return;
       
   244     }
       
   245     handler = \
       
   246       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
       
   247   }
       
   248 
       
   249   // Get the native function entry point
       
   250   address function;
       
   251   function = method->native_function();
       
   252   assert(function != NULL, "should be set if signature handler is");
       
   253 
       
   254   // Build the argument list
       
   255   if (handler->argument_count() * 2 > stack->available_words()) {
       
   256     Unimplemented();
       
   257   }
       
   258   void **arguments;
       
   259   void *mirror; {
       
   260     arguments =
       
   261       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
       
   262     void **dst = arguments;
       
   263 
       
   264     void *env = thread->jni_environment();
       
   265     *(dst++) = &env;
       
   266 
       
   267     if (method->is_static()) {
       
   268       istate->set_oop_temp(
       
   269         method->constants()->pool_holder()->klass_part()->java_mirror());
       
   270       mirror = istate->oop_temp_addr();
       
   271       *(dst++) = &mirror;
       
   272     }
       
   273 
       
   274     intptr_t *src = locals;
       
   275     for (int i = dst - arguments; i < handler->argument_count(); i++) {
       
   276       ffi_type *type = handler->argument_type(i);
       
   277       if (type == &ffi_type_pointer) {
       
   278         if (*src) {
       
   279           stack->push((intptr_t) src);
       
   280           *(dst++) = stack->sp();
       
   281         }
       
   282         else {
       
   283           *(dst++) = src;
       
   284         }
       
   285         src--;
       
   286       }
       
   287       else if (type->size == 4) {
       
   288         *(dst++) = src--;
       
   289       }
       
   290       else if (type->size == 8) {
       
   291         src--;
       
   292         *(dst++) = src--;
       
   293       }
       
   294       else {
       
   295         ShouldNotReachHere();
       
   296       }
       
   297     }
       
   298   }
       
   299 
       
   300   // Set up the Java frame anchor
       
   301   thread->set_last_Java_frame();
       
   302 
       
   303   // Change the thread state to _thread_in_native
       
   304   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
       
   305 
       
   306   // Make the call
       
   307   intptr_t result[4 - LogBytesPerWord];
       
   308   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
       
   309 
       
   310   // Change the thread state back to _thread_in_Java.
       
   311   // ThreadStateTransition::transition_from_native() cannot be used
       
   312   // here because it does not check for asynchronous exceptions.
       
   313   // We have to manage the transition ourself.
       
   314   thread->set_thread_state(_thread_in_native_trans);
       
   315 
       
   316   // Make sure new state is visible in the GC thread
       
   317   if (os::is_MP()) {
       
   318     if (UseMembar) {
       
   319       OrderAccess::fence();
       
   320     }
       
   321     else {
       
   322       InterfaceSupport::serialize_memory(thread);
       
   323     }
       
   324   }
       
   325 
       
   326   // Handle safepoint operations, pending suspend requests,
       
   327   // and pending asynchronous exceptions.
       
   328   if (SafepointSynchronize::do_call_back() ||
       
   329       thread->has_special_condition_for_native_trans()) {
       
   330     JavaThread::check_special_condition_for_native_trans(thread);
       
   331     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
       
   332   }
       
   333 
       
   334   // Finally we can change the thread state to _thread_in_Java.
       
   335   thread->set_thread_state(_thread_in_Java);
       
   336   fixup_after_potential_safepoint();
       
   337 
       
   338   // Clear the frame anchor
       
   339   thread->reset_last_Java_frame();
       
   340 
       
   341   // If the result was an oop then unbox it and store it in
       
   342   // oop_temp where the garbage collector can see it before
       
   343   // we release the handle it might be protected by.
       
   344   if (handler->result_type() == &ffi_type_pointer) {
       
   345     if (result[0])
       
   346       istate->set_oop_temp(*(oop *) result[0]);
       
   347     else
       
   348       istate->set_oop_temp(NULL);
       
   349   }
       
   350 
       
   351   // Reset handle block
       
   352   thread->active_handles()->clear();
       
   353 
       
   354   // Unlock if necessary.  It seems totally wrong that this
       
   355   // is skipped in the event of an exception but apparently
       
   356   // the template interpreter does this so we do too.
       
   357   if (monitor && !HAS_PENDING_EXCEPTION) {
       
   358     BasicLock *lock = monitor->lock();
       
   359     markOop header = lock->displaced_header();
       
   360     oop rcvr = monitor->obj();
       
   361     monitor->set_obj(NULL);
       
   362 
       
   363     if (header != NULL) {
       
   364       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
       
   365         monitor->set_obj(rcvr); {
       
   366           HandleMark hm(thread);
       
   367           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
       
   368         }
       
   369       }
       
   370     }
       
   371   }
       
   372 
       
   373  unwind_and_return:
       
   374 
       
   375   // Unwind the current activation
       
   376   thread->pop_zero_frame();
       
   377 
       
   378   // Pop our parameters
       
   379   stack->set_sp(stack->sp() + method->size_of_parameters());
       
   380 
       
   381   // Push our result
       
   382   if (!HAS_PENDING_EXCEPTION) {
       
   383     stack->set_sp(stack->sp() - type2size[method->result_type()]);
       
   384 
       
   385     switch (method->result_type()) {
       
   386     case T_VOID:
       
   387       break;
       
   388 
       
   389     case T_BOOLEAN:
       
   390 #ifndef VM_LITTLE_ENDIAN
       
   391       result[0] <<= (BitsPerWord - BitsPerByte);
       
   392 #endif
       
   393       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
       
   394       break;
       
   395 
       
   396     case T_CHAR:
       
   397 #ifndef VM_LITTLE_ENDIAN
       
   398       result[0] <<= (BitsPerWord - BitsPerShort);
       
   399 #endif
       
   400       SET_LOCALS_INT(*(jchar *) result, 0);
       
   401       break;
       
   402 
       
   403     case T_BYTE:
       
   404 #ifndef VM_LITTLE_ENDIAN
       
   405       result[0] <<= (BitsPerWord - BitsPerByte);
       
   406 #endif
       
   407       SET_LOCALS_INT(*(jbyte *) result, 0);
       
   408       break;
       
   409 
       
   410     case T_SHORT:
       
   411 #ifndef VM_LITTLE_ENDIAN
       
   412       result[0] <<= (BitsPerWord - BitsPerShort);
       
   413 #endif
       
   414       SET_LOCALS_INT(*(jshort *) result, 0);
       
   415       break;
       
   416 
       
   417     case T_INT:
       
   418 #ifndef VM_LITTLE_ENDIAN
       
   419       result[0] <<= (BitsPerWord - BitsPerInt);
       
   420 #endif
       
   421       SET_LOCALS_INT(*(jint *) result, 0);
       
   422       break;
       
   423 
       
   424     case T_LONG:
       
   425       SET_LOCALS_LONG(*(jlong *) result, 0);
       
   426       break;
       
   427 
       
   428     case T_FLOAT:
       
   429       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
       
   430       break;
       
   431 
       
   432     case T_DOUBLE:
       
   433       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
       
   434       break;
       
   435 
       
   436     case T_OBJECT:
       
   437     case T_ARRAY:
       
   438       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
       
   439       break;
       
   440 
       
   441     default:
       
   442       ShouldNotReachHere();
       
   443     }
       
   444   }
       
   445 }
       
   446 
       
   447 void CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
       
   448   JavaThread *thread = (JavaThread *) THREAD;
       
   449   ZeroStack *stack = thread->zero_stack();
       
   450   intptr_t *locals = stack->sp();
       
   451 
       
   452   // Drop into the slow path if we need a safepoint check
       
   453   if (SafepointSynchronize::do_call_back()) {
       
   454     normal_entry(method, 0, THREAD);
       
   455     return;
       
   456   }
       
   457 
       
   458   // Load the object pointer and drop into the slow path
       
   459   // if we have a NullPointerException
       
   460   oop object = LOCALS_OBJECT(0);
       
   461   if (object == NULL) {
       
   462     normal_entry(method, 0, THREAD);
       
   463     return;
       
   464   }
       
   465 
       
   466   // Read the field index from the bytecode, which looks like this:
       
   467   //  0:  aload_0
       
   468   //  1:  getfield
       
   469   //  2:    index
       
   470   //  3:    index
       
   471   //  4:  ireturn/areturn
       
   472   // NB this is not raw bytecode: index is in machine order
       
   473   u1 *code = method->code_base();
       
   474   assert(code[0] == Bytecodes::_aload_0 &&
       
   475          code[1] == Bytecodes::_getfield &&
       
   476          (code[4] == Bytecodes::_ireturn ||
       
   477           code[4] == Bytecodes::_areturn), "should do");
       
   478   u2 index = Bytes::get_native_u2(&code[2]);
       
   479 
       
   480   // Get the entry from the constant pool cache, and drop into
       
   481   // the slow path if it has not been resolved
       
   482   constantPoolCacheOop cache = method->constants()->cache();
       
   483   ConstantPoolCacheEntry* entry = cache->entry_at(index);
       
   484   if (!entry->is_resolved(Bytecodes::_getfield)) {
       
   485     normal_entry(method, 0, THREAD);
       
   486     return;
       
   487   }
       
   488 
       
   489   // Get the result and push it onto the stack
       
   490   switch (entry->flag_state()) {
       
   491   case ltos:
       
   492   case dtos:
       
   493     if (stack->available_words() < 1) {
       
   494       Unimplemented();
       
   495     }
       
   496     stack->alloc(wordSize);
       
   497     break;
       
   498   }
       
   499   if (entry->is_volatile()) {
       
   500     switch (entry->flag_state()) {
       
   501     case ctos:
       
   502       SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
       
   503       break;
       
   504 
       
   505     case btos:
       
   506       SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
       
   507       break;
       
   508 
       
   509     case stos:
       
   510       SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
       
   511       break;
       
   512 
       
   513     case itos:
       
   514       SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
       
   515       break;
       
   516 
       
   517     case ltos:
       
   518       SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
       
   519       break;
       
   520 
       
   521     case ftos:
       
   522       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
       
   523       break;
       
   524 
       
   525     case dtos:
       
   526       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
       
   527       break;
       
   528 
       
   529     case atos:
       
   530       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
       
   531       break;
       
   532 
       
   533     default:
       
   534       ShouldNotReachHere();
       
   535     }
       
   536   }
       
   537   else {
       
   538     switch (entry->flag_state()) {
       
   539     case ctos:
       
   540       SET_LOCALS_INT(object->char_field(entry->f2()), 0);
       
   541       break;
       
   542 
       
   543     case btos:
       
   544       SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
       
   545       break;
       
   546 
       
   547     case stos:
       
   548       SET_LOCALS_INT(object->short_field(entry->f2()), 0);
       
   549       break;
       
   550 
       
   551     case itos:
       
   552       SET_LOCALS_INT(object->int_field(entry->f2()), 0);
       
   553       break;
       
   554 
       
   555     case ltos:
       
   556       SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
       
   557       break;
       
   558 
       
   559     case ftos:
       
   560       SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
       
   561       break;
       
   562 
       
   563     case dtos:
       
   564       SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
       
   565       break;
       
   566 
       
   567     case atos:
       
   568       SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
       
   569       break;
       
   570 
       
   571     default:
       
   572       ShouldNotReachHere();
       
   573     }
       
   574   }
       
   575 }
       
   576 
       
   577 void CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
       
   578   JavaThread *thread = (JavaThread *) THREAD;
       
   579   ZeroStack *stack = thread->zero_stack();
       
   580 
       
   581   // Drop into the slow path if we need a safepoint check
       
   582   if (SafepointSynchronize::do_call_back()) {
       
   583     normal_entry(method, 0, THREAD);
       
   584     return;
       
   585   }
       
   586 
       
   587   // Pop our parameters
       
   588   stack->set_sp(stack->sp() + method->size_of_parameters());
       
   589 }
       
   590 
       
   591 bool CppInterpreter::stack_overflow_imminent(JavaThread *thread) {
       
   592   // How is the ABI stack?
       
   593   address stack_top = thread->stack_base() - thread->stack_size();
       
   594   int free_stack = os::current_stack_pointer() - stack_top;
       
   595   if (free_stack < StackShadowPages * os::vm_page_size()) {
       
   596     return true;
       
   597   }
       
   598 
       
   599   // How is the Zero stack?
       
   600   // Throwing a StackOverflowError involves a VM call, which means
       
   601   // we need a frame on the stack.  We should be checking here to
       
   602   // ensure that methods we call have enough room to install the
       
   603   // largest possible frame, but that's more than twice the size
       
   604   // of the entire Zero stack we get by default, so we just check
       
   605   // we have *some* space instead...
       
   606   free_stack = thread->zero_stack()->available_words() * wordSize;
       
   607   if (free_stack < StackShadowPages * os::vm_page_size()) {
       
   608     return true;
       
   609   }
       
   610 
       
   611   return false;
       
   612 }
       
   613 
       
   614 InterpreterFrame *InterpreterFrame::build(ZeroStack*       stack,
       
   615                                           const methodOop  method,
       
   616                                           JavaThread*      thread) {
       
   617   int monitor_words =
       
   618     method->is_synchronized() ? frame::interpreter_frame_monitor_size() : 0;
       
   619   int stack_words = method->is_native() ? 0 : method->max_stack();
       
   620 
       
   621   if (header_words + monitor_words + stack_words > stack->available_words()) {
       
   622     Unimplemented();
       
   623   }
       
   624 
       
   625   intptr_t *locals;
       
   626   if (method->is_native())
       
   627     locals = stack->sp() + (method->size_of_parameters() - 1);
       
   628   else
       
   629     locals = stack->sp() + (method->max_locals() - 1);
       
   630 
       
   631   stack->push(0); // next_frame, filled in later
       
   632   intptr_t *fp = stack->sp();
       
   633   assert(fp - stack->sp() == next_frame_off, "should be");
       
   634 
       
   635   stack->push(INTERPRETER_FRAME);
       
   636   assert(fp - stack->sp() == frame_type_off, "should be");
       
   637 
       
   638   interpreterState istate =
       
   639     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
       
   640   assert(fp - stack->sp() == istate_off, "should be");
       
   641 
       
   642   istate->set_locals(locals);
       
   643   istate->set_method(method);
       
   644   istate->set_self_link(istate);
       
   645   istate->set_prev_link(NULL);
       
   646   istate->set_thread(thread);
       
   647   istate->set_bcp(method->is_native() ? NULL : method->code_base());
       
   648   istate->set_constants(method->constants()->cache());
       
   649   istate->set_msg(BytecodeInterpreter::method_entry);
       
   650   istate->set_oop_temp(NULL);
       
   651   istate->set_mdx(NULL);
       
   652   istate->set_callee(NULL);
       
   653 
       
   654   istate->set_monitor_base((BasicObjectLock *) stack->sp());
       
   655   if (method->is_synchronized()) {
       
   656     BasicObjectLock *monitor =
       
   657       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
       
   658     oop object;
       
   659     if (method->is_static())
       
   660       object = method->constants()->pool_holder()->klass_part()->java_mirror();
       
   661     else
       
   662       object = (oop) locals[0];
       
   663     monitor->set_obj(object);
       
   664   }
       
   665 
       
   666   istate->set_stack_base(stack->sp());
       
   667   istate->set_stack(stack->sp() - 1);
       
   668   if (stack_words)
       
   669     stack->alloc(stack_words * wordSize);
       
   670   istate->set_stack_limit(stack->sp() - 1);
       
   671 
       
   672   return (InterpreterFrame *) fp;
       
   673 }
       
   674 
       
   675 int AbstractInterpreter::BasicType_as_index(BasicType type) {
       
   676   int i = 0;
       
   677   switch (type) {
       
   678     case T_BOOLEAN: i = 0; break;
       
   679     case T_CHAR   : i = 1; break;
       
   680     case T_BYTE   : i = 2; break;
       
   681     case T_SHORT  : i = 3; break;
       
   682     case T_INT    : i = 4; break;
       
   683     case T_LONG   : i = 5; break;
       
   684     case T_VOID   : i = 6; break;
       
   685     case T_FLOAT  : i = 7; break;
       
   686     case T_DOUBLE : i = 8; break;
       
   687     case T_OBJECT : i = 9; break;
       
   688     case T_ARRAY  : i = 9; break;
       
   689     default       : ShouldNotReachHere();
       
   690   }
       
   691   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
       
   692          "index out of bounds");
       
   693   return i;
       
   694 }
       
   695 
       
   696 address InterpreterGenerator::generate_empty_entry() {
       
   697   if (!UseFastEmptyMethods)
       
   698     return NULL;
       
   699 
       
   700   return generate_entry((address) CppInterpreter::empty_entry);
       
   701 }
       
   702 
       
   703 address InterpreterGenerator::generate_accessor_entry() {
       
   704   if (!UseFastAccessorMethods)
       
   705     return NULL;
       
   706 
       
   707   return generate_entry((address) CppInterpreter::accessor_entry);
       
   708 }
       
   709 
       
   710 address InterpreterGenerator::generate_native_entry(bool synchronized) {
       
   711   assert(synchronized == false, "should be");
       
   712 
       
   713   return generate_entry((address) CppInterpreter::native_entry);
       
   714 }
       
   715 
       
   716 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
       
   717   assert(synchronized == false, "should be");
       
   718 
       
   719   return generate_entry((address) CppInterpreter::normal_entry);
       
   720 }
       
   721 
       
   722 address AbstractInterpreterGenerator::generate_method_entry(
       
   723     AbstractInterpreter::MethodKind kind) {
       
   724   address entry_point = NULL;
       
   725 
       
   726   switch (kind) {
       
   727   case Interpreter::zerolocals:
       
   728   case Interpreter::zerolocals_synchronized:
       
   729     break;
       
   730 
       
   731   case Interpreter::native:
       
   732     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
       
   733     break;
       
   734 
       
   735   case Interpreter::native_synchronized:
       
   736     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
       
   737     break;
       
   738 
       
   739   case Interpreter::empty:
       
   740     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
       
   741     break;
       
   742 
       
   743   case Interpreter::accessor:
       
   744     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
       
   745     break;
       
   746 
       
   747   case Interpreter::abstract:
       
   748     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
       
   749     break;
       
   750 
       
   751   case Interpreter::method_handle:
       
   752     entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
       
   753     break;
       
   754 
       
   755   case Interpreter::java_lang_math_sin:
       
   756   case Interpreter::java_lang_math_cos:
       
   757   case Interpreter::java_lang_math_tan:
       
   758   case Interpreter::java_lang_math_abs:
       
   759   case Interpreter::java_lang_math_log:
       
   760   case Interpreter::java_lang_math_log10:
       
   761   case Interpreter::java_lang_math_sqrt:
       
   762     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
       
   763     break;
       
   764 
       
   765   default:
       
   766     ShouldNotReachHere();
       
   767   }
       
   768 
       
   769   if (entry_point == NULL)
       
   770     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
       
   771 
       
   772   return entry_point;
       
   773 }
       
   774 
       
   775 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
       
   776  : CppInterpreterGenerator(code) {
       
   777    generate_all();
       
   778 }
       
   779 
       
   780 // Deoptimization helpers
       
   781 
       
   782 InterpreterFrame *InterpreterFrame::build(ZeroStack* stack, int size) {
       
   783   int size_in_words = size >> LogBytesPerWord;
       
   784   assert(size_in_words * wordSize == size, "unaligned");
       
   785   assert(size_in_words >= header_words, "too small");
       
   786 
       
   787   if (size_in_words > stack->available_words()) {
       
   788     Unimplemented();
       
   789   }
       
   790 
       
   791   stack->push(0); // next_frame, filled in later
       
   792   intptr_t *fp = stack->sp();
       
   793   assert(fp - stack->sp() == next_frame_off, "should be");
       
   794 
       
   795   stack->push(INTERPRETER_FRAME);
       
   796   assert(fp - stack->sp() == frame_type_off, "should be");
       
   797 
       
   798   interpreterState istate =
       
   799     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
       
   800   assert(fp - stack->sp() == istate_off, "should be");
       
   801   istate->set_self_link(NULL); // mark invalid
       
   802 
       
   803   stack->alloc((size_in_words - header_words) * wordSize);
       
   804 
       
   805   return (InterpreterFrame *) fp;
       
   806 }
       
   807 
       
   808 int AbstractInterpreter::layout_activation(methodOop method,
       
   809                                            int       tempcount,
       
   810                                            int       popframe_extra_args,
       
   811                                            int       moncount,
       
   812                                            int       callee_param_count,
       
   813                                            int       callee_locals,
       
   814                                            frame*    caller,
       
   815                                            frame*    interpreter_frame,
       
   816                                            bool      is_top_frame) {
       
   817   assert(popframe_extra_args == 0, "what to do?");
       
   818   assert(!is_top_frame || (!callee_locals && !callee_param_count),
       
   819          "top frame should have no caller")
       
   820 
       
   821   // This code must exactly match what InterpreterFrame::build
       
   822   // does (the full InterpreterFrame::build, that is, not the
       
   823   // one that creates empty frames for the deoptimizer).
       
   824   //
       
   825   // If interpreter_frame is not NULL then it will be filled in.
       
   826   // It's size is determined by a previous call to this method,
       
   827   // so it should be correct.
       
   828   //
       
   829   // Note that tempcount is the current size of the expression
       
   830   // stack.  For top most frames we will allocate a full sized
       
   831   // expression stack and not the trimmed version that non-top
       
   832   // frames have.
       
   833 
       
   834   int header_words        = InterpreterFrame::header_words;
       
   835   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
       
   836   int stack_words         = is_top_frame ? method->max_stack() : tempcount;
       
   837   int callee_extra_locals = callee_locals - callee_param_count;
       
   838 
       
   839   if (interpreter_frame) {
       
   840     intptr_t *locals        = interpreter_frame->sp() + method->max_locals();
       
   841     interpreterState istate = interpreter_frame->get_interpreterState();
       
   842     intptr_t *monitor_base  = (intptr_t*) istate;
       
   843     intptr_t *stack_base    = monitor_base - monitor_words;
       
   844     intptr_t *stack         = stack_base - tempcount - 1;
       
   845 
       
   846     BytecodeInterpreter::layout_interpreterState(istate,
       
   847                                                  caller,
       
   848                                                  NULL,
       
   849                                                  method,
       
   850                                                  locals,
       
   851                                                  stack,
       
   852                                                  stack_base,
       
   853                                                  monitor_base,
       
   854                                                  NULL,
       
   855                                                  is_top_frame);
       
   856   }
       
   857   return header_words + monitor_words + stack_words + callee_extra_locals;
       
   858 }
       
   859 
       
   860 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
       
   861                                                   frame*    caller,
       
   862                                                   frame*    current,
       
   863                                                   methodOop method,
       
   864                                                   intptr_t* locals,
       
   865                                                   intptr_t* stack,
       
   866                                                   intptr_t* stack_base,
       
   867                                                   intptr_t* monitor_base,
       
   868                                                   intptr_t* frame_bottom,
       
   869                                                   bool      is_top_frame) {
       
   870   istate->set_locals(locals);
       
   871   istate->set_method(method);
       
   872   istate->set_self_link(istate);
       
   873   istate->set_prev_link(NULL);
       
   874   // thread will be set by a hacky repurposing of frame::patch_pc()
       
   875   // bcp will be set by vframeArrayElement::unpack_on_stack()
       
   876   istate->set_constants(method->constants()->cache());
       
   877   istate->set_msg(BytecodeInterpreter::method_resume);
       
   878   istate->set_bcp_advance(0);
       
   879   istate->set_oop_temp(NULL);
       
   880   istate->set_mdx(NULL);
       
   881   if (caller->is_interpreted_frame()) {
       
   882     interpreterState prev = caller->get_interpreterState();
       
   883     prev->set_callee(method);
       
   884     if (*prev->bcp() == Bytecodes::_invokeinterface)
       
   885       prev->set_bcp_advance(5);
       
   886     else
       
   887       prev->set_bcp_advance(3);
       
   888   }
       
   889   istate->set_callee(NULL);
       
   890   istate->set_monitor_base((BasicObjectLock *) monitor_base);
       
   891   istate->set_stack_base(stack_base);
       
   892   istate->set_stack(stack);
       
   893   istate->set_stack_limit(stack_base - method->max_stack() - 1);
       
   894 }
       
   895 
       
   896 address CppInterpreter::return_entry(TosState state, int length) {
       
   897   ShouldNotCallThis();
       
   898 }
       
   899 
       
   900 address CppInterpreter::deopt_entry(TosState state, int length) {
       
   901   return NULL;
       
   902 }
       
   903 
       
   904 // Helper for (runtime) stack overflow checks
       
   905 
       
   906 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
       
   907   return 0;
       
   908 }
       
   909 
       
   910 // Helper for figuring out if frames are interpreter frames
       
   911 
       
   912 bool CppInterpreter::contains(address pc) {
       
   913 #ifdef PRODUCT
       
   914   ShouldNotCallThis();
       
   915 #else
       
   916   return false; // make frame::print_value_on work
       
   917 #endif // !PRODUCT
       
   918 }
       
   919 
       
   920 // Result handlers and convertors
       
   921 
       
   922 address CppInterpreterGenerator::generate_result_handler_for(
       
   923     BasicType type) {
       
   924   assembler()->advance(1);
       
   925   return ShouldNotCallThisStub();
       
   926 }
       
   927 
       
   928 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
       
   929     BasicType type) {
       
   930   assembler()->advance(1);
       
   931   return ShouldNotCallThisStub();
       
   932 }
       
   933 
       
   934 address CppInterpreterGenerator::generate_stack_to_stack_converter(
       
   935     BasicType type) {
       
   936   assembler()->advance(1);
       
   937   return ShouldNotCallThisStub();
       
   938 }
       
   939 
       
   940 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
       
   941     BasicType type) {
       
   942   assembler()->advance(1);
       
   943   return ShouldNotCallThisStub();
       
   944 }
       
   945 
       
   946 #endif // CC_INTERP