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   // Update the invocation counter
 208   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
 209     thread->set_do_not_unlock();
 210     InvocationCounter *counter = method->invocation_counter();
 211     counter->increment();
 212     if (counter->reached_InvocationLimit()) {
 213       CALL_VM_NOCHECK(
 214         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
 215       if (HAS_PENDING_EXCEPTION)
 216         goto unwind_and_return;
 217     }
 218     thread->clr_do_not_unlock();
 219   }
 220 
 221   // Lock if necessary
 222   BasicObjectLock *monitor;
 223   monitor = NULL;
 224   if (method->is_synchronized()) {
 225     monitor = (BasicObjectLock*) istate->stack_base();
 226     oop lockee = monitor->obj();
 227     markOop disp = lockee->mark()->set_unlocked();
 228 
 229     monitor->lock()->set_displaced_header(disp);
 230     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
 231       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
 232         monitor->lock()->set_displaced_header(NULL);
 233       }
 234       else {
 235         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
 236         if (HAS_PENDING_EXCEPTION)
 237           goto unwind_and_return;
 238       }
 239     }
 240   }
 241 
 242   // Get the signature handler
 243   InterpreterRuntime::SignatureHandler *handler; {
 244     address handlerAddr = method->signature_handler();
 245     if (handlerAddr == NULL) {
 246       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
 247       if (HAS_PENDING_EXCEPTION)
 248         goto unwind_and_return;
 249 
 250       handlerAddr = method->signature_handler();
 251       assert(handlerAddr != NULL, "eh?");
 252     }
 253     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
 254       CALL_VM_NOCHECK(handlerAddr =
 255         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
 256       if (HAS_PENDING_EXCEPTION)
 257         goto unwind_and_return;
 258     }
 259     handler = \
 260       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
 261   }
 262 
 263   // Get the native function entry point
 264   address function;
 265   function = method->native_function();
 266   assert(function != NULL, "should be set if signature handler is");
 267 
 268   // Build the argument list
 269   if (handler->argument_count() * 2 > stack->available_words()) {
 270     Unimplemented();
 271   }
 272   void **arguments;
 273   void *mirror; {
 274     arguments =
 275       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
 276     void **dst = arguments;
 277 
 278     void *env = thread->jni_environment();
 279     *(dst++) = &env;
 280 
 281     if (method->is_static()) {
 282       istate->set_oop_temp(
 283         method->constants()->pool_holder()->klass_part()->java_mirror());
 284       mirror = istate->oop_temp_addr();
 285       *(dst++) = &mirror;
 286     }
 287 
 288     intptr_t *src = locals;
 289     for (int i = dst - arguments; i < handler->argument_count(); i++) {
 290       ffi_type *type = handler->argument_type(i);
 291       if (type == &ffi_type_pointer) {
 292         if (*src) {
 293           stack->push((intptr_t) src);
 294           *(dst++) = stack->sp();
 295         }
 296         else {
 297           *(dst++) = src;
 298         }
 299         src--;
 300       }
 301       else if (type->size == 4) {
 302         *(dst++) = src--;
 303       }
 304       else if (type->size == 8) {
 305         src--;
 306         *(dst++) = src--;
 307       }
 308       else {
 309         ShouldNotReachHere();
 310       }
 311     }
 312   }
 313 
 314   // Set up the Java frame anchor
 315   thread->set_last_Java_frame();
 316 
 317   // Change the thread state to _thread_in_native
 318   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
 319 
 320   // Make the call
 321   intptr_t result[4 - LogBytesPerWord];
 322   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
 323 
 324   // Change the thread state back to _thread_in_Java.
 325   // ThreadStateTransition::transition_from_native() cannot be used
 326   // here because it does not check for asynchronous exceptions.
 327   // We have to manage the transition ourself.
 328   thread->set_thread_state(_thread_in_native_trans);
 329 
 330   // Make sure new state is visible in the GC thread
 331   if (os::is_MP()) {
 332     if (UseMembar) {
 333       OrderAccess::fence();
 334     }
 335     else {
 336       InterfaceSupport::serialize_memory(thread);
 337     }
 338   }
 339 
 340   // Handle safepoint operations, pending suspend requests,
 341   // and pending asynchronous exceptions.
 342   if (SafepointSynchronize::do_call_back() ||
 343       thread->has_special_condition_for_native_trans()) {
 344     JavaThread::check_special_condition_for_native_trans(thread);
 345     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
 346   }
 347 
 348   // Finally we can change the thread state to _thread_in_Java.
 349   thread->set_thread_state(_thread_in_Java);
 350   fixup_after_potential_safepoint();
 351 
 352   // Clear the frame anchor
 353   thread->reset_last_Java_frame();
 354 
 355   // If the result was an oop then unbox it and store it in
 356   // oop_temp where the garbage collector can see it before
 357   // we release the handle it might be protected by.
 358   if (handler->result_type() == &ffi_type_pointer) {
 359     if (result[0])
 360       istate->set_oop_temp(*(oop *) result[0]);
 361     else
 362       istate->set_oop_temp(NULL);
 363   }
 364 
 365   // Reset handle block
 366   thread->active_handles()->clear();
 367 
 368   // Unlock if necessary.  It seems totally wrong that this
 369   // is skipped in the event of an exception but apparently
 370   // the template interpreter does this so we do too.
 371   if (monitor && !HAS_PENDING_EXCEPTION) {
 372     BasicLock *lock = monitor->lock();
 373     markOop header = lock->displaced_header();
 374     oop rcvr = monitor->obj();
 375     monitor->set_obj(NULL);
 376 
 377     if (header != NULL) {
 378       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
 379         monitor->set_obj(rcvr); {
 380           HandleMark hm(thread);
 381           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
 382         }
 383       }
 384     }
 385   }
 386 
 387  unwind_and_return:
 388 
 389   // Unwind the current activation
 390   thread->pop_zero_frame();
 391 
 392   // Pop our parameters
 393   stack->set_sp(stack->sp() + method->size_of_parameters());
 394 
 395   // Push our result
 396   if (!HAS_PENDING_EXCEPTION) {
 397     stack->set_sp(stack->sp() - type2size[method->result_type()]);
 398 
 399     switch (method->result_type()) {
 400     case T_VOID:
 401       break;
 402 
 403     case T_BOOLEAN:
 404 #ifndef VM_LITTLE_ENDIAN
 405       result[0] <<= (BitsPerWord - BitsPerByte);
 406 #endif
 407       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
 408       break;
 409 
 410     case T_CHAR:
 411 #ifndef VM_LITTLE_ENDIAN
 412       result[0] <<= (BitsPerWord - BitsPerShort);
 413 #endif
 414       SET_LOCALS_INT(*(jchar *) result, 0);
 415       break;
 416 
 417     case T_BYTE:
 418 #ifndef VM_LITTLE_ENDIAN
 419       result[0] <<= (BitsPerWord - BitsPerByte);
 420 #endif
 421       SET_LOCALS_INT(*(jbyte *) result, 0);
 422       break;
 423 
 424     case T_SHORT:
 425 #ifndef VM_LITTLE_ENDIAN
 426       result[0] <<= (BitsPerWord - BitsPerShort);
 427 #endif
 428       SET_LOCALS_INT(*(jshort *) result, 0);
 429       break;
 430 
 431     case T_INT:
 432 #ifndef VM_LITTLE_ENDIAN
 433       result[0] <<= (BitsPerWord - BitsPerInt);
 434 #endif
 435       SET_LOCALS_INT(*(jint *) result, 0);
 436       break;
 437 
 438     case T_LONG:
 439       SET_LOCALS_LONG(*(jlong *) result, 0);
 440       break;
 441 
 442     case T_FLOAT:
 443       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
 444       break;
 445 
 446     case T_DOUBLE:
 447       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
 448       break;
 449 
 450     case T_OBJECT:
 451     case T_ARRAY:
 452       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
 453       break;
 454 
 455     default:
 456       ShouldNotReachHere();
 457     }
 458   }
 459 }
 460 
 461 void CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 462   JavaThread *thread = (JavaThread *) THREAD;
 463   ZeroStack *stack = thread->zero_stack();
 464   intptr_t *locals = stack->sp();
 465 
 466   // Drop into the slow path if we need a safepoint check
 467   if (SafepointSynchronize::do_call_back()) {
 468     normal_entry(method, 0, THREAD);
 469     return;
 470   }
 471 
 472   // Load the object pointer and drop into the slow path
 473   // if we have a NullPointerException
 474   oop object = LOCALS_OBJECT(0);
 475   if (object == NULL) {
 476     normal_entry(method, 0, THREAD);
 477     return;
 478   }
 479 
 480   // Read the field index from the bytecode, which looks like this:
 481   //  0:  aload_0
 482   //  1:  getfield
 483   //  2:    index
 484   //  3:    index
 485   //  4:  ireturn/areturn
 486   // NB this is not raw bytecode: index is in machine order
 487   u1 *code = method->code_base();
 488   assert(code[0] == Bytecodes::_aload_0 &&
 489          code[1] == Bytecodes::_getfield &&
 490          (code[4] == Bytecodes::_ireturn ||
 491           code[4] == Bytecodes::_areturn), "should do");
 492   u2 index = Bytes::get_native_u2(&code[2]);
 493 
 494   // Get the entry from the constant pool cache, and drop into
 495   // the slow path if it has not been resolved
 496   constantPoolCacheOop cache = method->constants()->cache();
 497   ConstantPoolCacheEntry* entry = cache->entry_at(index);
 498   if (!entry->is_resolved(Bytecodes::_getfield)) {
 499     normal_entry(method, 0, THREAD);
 500     return;
 501   }
 502 
 503   // Get the result and push it onto the stack
 504   switch (entry->flag_state()) {
 505   case ltos:
 506   case dtos:
 507     if (stack->available_words() < 1) {
 508       Unimplemented();
 509     }
 510     stack->alloc(wordSize);
 511     break;
 512   }
 513   if (entry->is_volatile()) {
 514     switch (entry->flag_state()) {
 515     case ctos:
 516       SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
 517       break;
 518 
 519     case btos:
 520       SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
 521       break;
 522 
 523     case stos:
 524       SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
 525       break;
 526 
 527     case itos:
 528       SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
 529       break;
 530 
 531     case ltos:
 532       SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
 533       break;
 534 
 535     case ftos:
 536       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
 537       break;
 538 
 539     case dtos:
 540       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
 541       break;
 542 
 543     case atos:
 544       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
 545       break;
 546 
 547     default:
 548       ShouldNotReachHere();
 549     }
 550   }
 551   else {
 552     switch (entry->flag_state()) {
 553     case ctos:
 554       SET_LOCALS_INT(object->char_field(entry->f2()), 0);
 555       break;
 556 
 557     case btos:
 558       SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
 559       break;
 560 
 561     case stos:
 562       SET_LOCALS_INT(object->short_field(entry->f2()), 0);
 563       break;
 564 
 565     case itos:
 566       SET_LOCALS_INT(object->int_field(entry->f2()), 0);
 567       break;
 568 
 569     case ltos:
 570       SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
 571       break;
 572 
 573     case ftos:
 574       SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
 575       break;
 576 
 577     case dtos:
 578       SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
 579       break;
 580 
 581     case atos:
 582       SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
 583       break;
 584 
 585     default:
 586       ShouldNotReachHere();
 587     }
 588   }
 589 }
 590 
 591 void CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 592   JavaThread *thread = (JavaThread *) THREAD;
 593   ZeroStack *stack = thread->zero_stack();
 594 
 595   // Drop into the slow path if we need a safepoint check
 596   if (SafepointSynchronize::do_call_back()) {
 597     normal_entry(method, 0, THREAD);
 598     return;
 599   }
 600 
 601   // Pop our parameters
 602   stack->set_sp(stack->sp() + method->size_of_parameters());
 603 }
 604 
 605 bool CppInterpreter::stack_overflow_imminent(JavaThread *thread) {
 606   // How is the ABI stack?
 607   address stack_top = thread->stack_base() - thread->stack_size();
 608   int free_stack = os::current_stack_pointer() - stack_top;
 609   if (free_stack < StackShadowPages * os::vm_page_size()) {
 610     return true;
 611   }
 612 
 613   // How is the Zero stack?
 614   // Throwing a StackOverflowError involves a VM call, which means
 615   // we need a frame on the stack.  We should be checking here to
 616   // ensure that methods we call have enough room to install the
 617   // largest possible frame, but that's more than twice the size
 618   // of the entire Zero stack we get by default, so we just check
 619   // we have *some* space instead...
 620   free_stack = thread->zero_stack()->available_words() * wordSize;
 621   if (free_stack < StackShadowPages * os::vm_page_size()) {
 622     return true;
 623   }
 624 
 625   return false;
 626 }
 627 
 628 InterpreterFrame *InterpreterFrame::build(ZeroStack*       stack,
 629                                           const methodOop  method,
 630                                           JavaThread*      thread) {
 631   int monitor_words =
 632     method->is_synchronized() ? frame::interpreter_frame_monitor_size() : 0;
 633   int stack_words = method->is_native() ? 0 : method->max_stack();
 634 
 635   if (header_words + monitor_words + stack_words > stack->available_words()) {
 636     Unimplemented();
 637   }
 638 
 639   intptr_t *locals;
 640   if (method->is_native())
 641     locals = stack->sp() + (method->size_of_parameters() - 1);
 642   else
 643     locals = stack->sp() + (method->max_locals() - 1);
 644 
 645   stack->push(0); // next_frame, filled in later
 646   intptr_t *fp = stack->sp();
 647   assert(fp - stack->sp() == next_frame_off, "should be");
 648 
 649   stack->push(INTERPRETER_FRAME);
 650   assert(fp - stack->sp() == frame_type_off, "should be");
 651 
 652   interpreterState istate =
 653     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 654   assert(fp - stack->sp() == istate_off, "should be");
 655 
 656   istate->set_locals(locals);
 657   istate->set_method(method);
 658   istate->set_self_link(istate);
 659   istate->set_prev_link(NULL);
 660   istate->set_thread(thread);
 661   istate->set_bcp(method->is_native() ? NULL : method->code_base());
 662   istate->set_constants(method->constants()->cache());
 663   istate->set_msg(BytecodeInterpreter::method_entry);
 664   istate->set_oop_temp(NULL);
 665   istate->set_mdx(NULL);
 666   istate->set_callee(NULL);
 667 
 668   istate->set_monitor_base((BasicObjectLock *) stack->sp());
 669   if (method->is_synchronized()) {
 670     BasicObjectLock *monitor =
 671       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
 672     oop object;
 673     if (method->is_static())
 674       object = method->constants()->pool_holder()->klass_part()->java_mirror();
 675     else
 676       object = (oop) locals[0];
 677     monitor->set_obj(object);
 678   }
 679 
 680   istate->set_stack_base(stack->sp());
 681   istate->set_stack(stack->sp() - 1);
 682   if (stack_words)
 683     stack->alloc(stack_words * wordSize);
 684   istate->set_stack_limit(stack->sp() - 1);
 685 
 686   return (InterpreterFrame *) fp;
 687 }
 688 
 689 int AbstractInterpreter::BasicType_as_index(BasicType type) {
 690   int i = 0;
 691   switch (type) {
 692     case T_BOOLEAN: i = 0; break;
 693     case T_CHAR   : i = 1; break;
 694     case T_BYTE   : i = 2; break;
 695     case T_SHORT  : i = 3; break;
 696     case T_INT    : i = 4; break;
 697     case T_LONG   : i = 5; break;
 698     case T_VOID   : i = 6; break;
 699     case T_FLOAT  : i = 7; break;
 700     case T_DOUBLE : i = 8; break;
 701     case T_OBJECT : i = 9; break;
 702     case T_ARRAY  : i = 9; break;
 703     default       : ShouldNotReachHere();
 704   }
 705   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
 706          "index out of bounds");
 707   return i;
 708 }
 709 
 710 address InterpreterGenerator::generate_empty_entry() {
 711   if (!UseFastEmptyMethods)
 712     return NULL;
 713 
 714   return generate_entry((address) CppInterpreter::empty_entry);
 715 }
 716 
 717 address InterpreterGenerator::generate_accessor_entry() {
 718   if (!UseFastAccessorMethods)
 719     return NULL;
 720 
 721   return generate_entry((address) CppInterpreter::accessor_entry);
 722 }
 723 
 724 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 725   assert(synchronized == false, "should be");
 726 
 727   return generate_entry((address) CppInterpreter::native_entry);
 728 }
 729 
 730 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
 731   assert(synchronized == false, "should be");
 732 
 733   return generate_entry((address) CppInterpreter::normal_entry);
 734 }
 735 
 736 address AbstractInterpreterGenerator::generate_method_entry(
 737     AbstractInterpreter::MethodKind kind) {
 738   address entry_point = NULL;
 739 
 740   switch (kind) {
 741   case Interpreter::zerolocals:
 742   case Interpreter::zerolocals_synchronized:
 743     break;
 744 
 745   case Interpreter::native:
 746     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
 747     break;
 748 
 749   case Interpreter::native_synchronized:
 750     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
 751     break;
 752 
 753   case Interpreter::empty:
 754     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
 755     break;
 756 
 757   case Interpreter::accessor:
 758     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
 759     break;
 760 
 761   case Interpreter::abstract:
 762     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
 763     break;
 764 
 765   case Interpreter::method_handle:
 766     entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
 767     break;
 768 
 769   case Interpreter::java_lang_math_sin:
 770   case Interpreter::java_lang_math_cos:
 771   case Interpreter::java_lang_math_tan:
 772   case Interpreter::java_lang_math_abs:
 773   case Interpreter::java_lang_math_log:
 774   case Interpreter::java_lang_math_log10:
 775   case Interpreter::java_lang_math_sqrt:
 776     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
 777     break;
 778 
 779   default:
 780     ShouldNotReachHere();
 781   }
 782 
 783   if (entry_point == NULL)
 784     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
 785 
 786   return entry_point;
 787 }
 788 
 789 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
 790  : CppInterpreterGenerator(code) {
 791    generate_all();
 792 }
 793 
 794 // Deoptimization helpers
 795 
 796 InterpreterFrame *InterpreterFrame::build(ZeroStack* stack, int size) {
 797   int size_in_words = size >> LogBytesPerWord;
 798   assert(size_in_words * wordSize == size, "unaligned");
 799   assert(size_in_words >= header_words, "too small");
 800 
 801   if (size_in_words > stack->available_words()) {
 802     Unimplemented();
 803   }
 804 
 805   stack->push(0); // next_frame, filled in later
 806   intptr_t *fp = stack->sp();
 807   assert(fp - stack->sp() == next_frame_off, "should be");
 808 
 809   stack->push(INTERPRETER_FRAME);
 810   assert(fp - stack->sp() == frame_type_off, "should be");
 811 
 812   interpreterState istate =
 813     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 814   assert(fp - stack->sp() == istate_off, "should be");
 815   istate->set_self_link(NULL); // mark invalid
 816 
 817   stack->alloc((size_in_words - header_words) * wordSize);
 818 
 819   return (InterpreterFrame *) fp;
 820 }
 821 
 822 int AbstractInterpreter::layout_activation(methodOop method,
 823                                            int       tempcount,
 824                                            int       popframe_extra_args,
 825                                            int       moncount,
 826                                            int       callee_param_count,
 827                                            int       callee_locals,
 828                                            frame*    caller,
 829                                            frame*    interpreter_frame,
 830                                            bool      is_top_frame) {
 831   assert(popframe_extra_args == 0, "what to do?");
 832   assert(!is_top_frame || (!callee_locals && !callee_param_count),
 833          "top frame should have no caller")
 834 
 835   // This code must exactly match what InterpreterFrame::build
 836   // does (the full InterpreterFrame::build, that is, not the
 837   // one that creates empty frames for the deoptimizer).
 838   //
 839   // If interpreter_frame is not NULL then it will be filled in.
 840   // It's size is determined by a previous call to this method,
 841   // so it should be correct.
 842   //
 843   // Note that tempcount is the current size of the expression
 844   // stack.  For top most frames we will allocate a full sized
 845   // expression stack and not the trimmed version that non-top
 846   // frames have.
 847 
 848   int header_words        = InterpreterFrame::header_words;
 849   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
 850   int stack_words         = is_top_frame ? method->max_stack() : tempcount;
 851   int callee_extra_locals = callee_locals - callee_param_count;
 852 
 853   if (interpreter_frame) {
 854     intptr_t *locals        = interpreter_frame->sp() + method->max_locals();
 855     interpreterState istate = interpreter_frame->get_interpreterState();
 856     intptr_t *monitor_base  = (intptr_t*) istate;
 857     intptr_t *stack_base    = monitor_base - monitor_words;
 858     intptr_t *stack         = stack_base - tempcount - 1;
 859 
 860     BytecodeInterpreter::layout_interpreterState(istate,
 861                                                  caller,
 862                                                  NULL,
 863                                                  method,
 864                                                  locals,
 865                                                  stack,
 866                                                  stack_base,
 867                                                  monitor_base,
 868                                                  NULL,
 869                                                  is_top_frame);
 870   }
 871   return header_words + monitor_words + stack_words + callee_extra_locals;
 872 }
 873 
 874 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
 875                                                   frame*    caller,
 876                                                   frame*    current,
 877                                                   methodOop method,
 878                                                   intptr_t* locals,
 879                                                   intptr_t* stack,
 880                                                   intptr_t* stack_base,
 881                                                   intptr_t* monitor_base,
 882                                                   intptr_t* frame_bottom,
 883                                                   bool      is_top_frame) {
 884   istate->set_locals(locals);
 885   istate->set_method(method);
 886   istate->set_self_link(istate);
 887   istate->set_prev_link(NULL);
 888   // thread will be set by a hacky repurposing of frame::patch_pc()
 889   // bcp will be set by vframeArrayElement::unpack_on_stack()
 890   istate->set_constants(method->constants()->cache());
 891   istate->set_msg(BytecodeInterpreter::method_resume);
 892   istate->set_bcp_advance(0);
 893   istate->set_oop_temp(NULL);
 894   istate->set_mdx(NULL);
 895   if (caller->is_interpreted_frame()) {
 896     interpreterState prev = caller->get_interpreterState();
 897     prev->set_callee(method);
 898     if (*prev->bcp() == Bytecodes::_invokeinterface)
 899       prev->set_bcp_advance(5);
 900     else
 901       prev->set_bcp_advance(3);
 902   }
 903   istate->set_callee(NULL);
 904   istate->set_monitor_base((BasicObjectLock *) monitor_base);
 905   istate->set_stack_base(stack_base);
 906   istate->set_stack(stack);
 907   istate->set_stack_limit(stack_base - method->max_stack() - 1);
 908 }
 909 
 910 address CppInterpreter::return_entry(TosState state, int length) {
 911   ShouldNotCallThis();
 912 }
 913 
 914 address CppInterpreter::deopt_entry(TosState state, int length) {
 915   return NULL;
 916 }
 917 
 918 // Helper for (runtime) stack overflow checks
 919 
 920 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
 921   return 0;
 922 }
 923 
 924 // Helper for figuring out if frames are interpreter frames
 925 
 926 bool CppInterpreter::contains(address pc) {
 927 #ifdef PRODUCT
 928   ShouldNotCallThis();
 929 #else
 930   return false; // make frame::print_value_on work
 931 #endif // !PRODUCT
 932 }
 933 
 934 // Result handlers and convertors
 935 
 936 address CppInterpreterGenerator::generate_result_handler_for(
 937     BasicType type) {
 938   assembler()->advance(1);
 939   return ShouldNotCallThisStub();
 940 }
 941 
 942 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
 943     BasicType type) {
 944   assembler()->advance(1);
 945   return ShouldNotCallThisStub();
 946 }
 947 
 948 address CppInterpreterGenerator::generate_stack_to_stack_converter(
 949     BasicType type) {
 950   assembler()->advance(1);
 951   return ShouldNotCallThisStub();
 952 }
 953 
 954 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
 955     BasicType type) {
 956   assembler()->advance(1);
 957   return ShouldNotCallThisStub();
 958 }
 959 
 960 #endif // CC_INTERP