1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009, 2010 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/assembler.hpp"
  28 #include "interpreter/bytecodeHistogram.hpp"
  29 #include "interpreter/cppInterpreter.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterGenerator.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/methodDataOop.hpp"
  35 #include "oops/methodOop.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/jvmtiThreadState.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/interfaceSupport.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/stubRoutines.hpp"
  45 #include "runtime/synchronizer.hpp"
  46 #include "runtime/timer.hpp"
  47 #include "runtime/vframeArray.hpp"
  48 #include "stack_zero.inline.hpp"
  49 #include "utilities/debug.hpp"
  50 #ifdef SHARK
  51 #include "shark/shark_globals.hpp"
  52 #endif
  53 
  54 #ifdef CC_INTERP
  55 
  56 #define fixup_after_potential_safepoint()       \
  57   method = istate->method()
  58 
  59 #define CALL_VM_NOCHECK(func)                   \
  60   thread->set_last_Java_frame();                \
  61   func;                                         \
  62   thread->reset_last_Java_frame();              \
  63   fixup_after_potential_safepoint()
  64 
  65 int CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
  66   JavaThread *thread = (JavaThread *) THREAD;
  67 
  68   // Allocate and initialize our frame.
  69   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
  70   thread->push_zero_frame(frame);
  71 
  72   // Execute those bytecodes!
  73   main_loop(0, THREAD);
  74 
  75   // No deoptimized frames on the stack
  76   return 0;
  77 }
  78 
  79 void CppInterpreter::main_loop(int recurse, TRAPS) {
  80   JavaThread *thread = (JavaThread *) THREAD;
  81   ZeroStack *stack = thread->zero_stack();
  82 
  83   // If we are entering from a deopt we may need to call
  84   // ourself a few times in order to get to our frame.
  85   if (recurse)
  86     main_loop(recurse - 1, THREAD);
  87 
  88   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  89   interpreterState istate = frame->interpreter_state();
  90   methodOop method = istate->method();
  91 
  92   intptr_t *result = NULL;
  93   int result_slots = 0;
  94 
  95   while (true) {
  96     // We can set up the frame anchor with everything we want at
  97     // this point as we are thread_in_Java and no safepoints can
  98     // occur until we go to vm mode.  We do have to clear flags
  99     // on return from vm but that is it.
 100     thread->set_last_Java_frame();
 101 
 102     // Call the interpreter
 103     if (JvmtiExport::can_post_interpreter_events())
 104       BytecodeInterpreter::runWithChecks(istate);
 105     else
 106       BytecodeInterpreter::run(istate);
 107     fixup_after_potential_safepoint();
 108 
 109     // Clear the frame anchor
 110     thread->reset_last_Java_frame();
 111 
 112     // Examine the message from the interpreter to decide what to do
 113     if (istate->msg() == BytecodeInterpreter::call_method) {
 114       methodOop callee = istate->callee();
 115 
 116       // Trim back the stack to put the parameters at the top
 117       stack->set_sp(istate->stack() + 1);
 118 
 119       // Make the call
 120       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
 121       fixup_after_potential_safepoint();
 122 
 123       // Convert the result
 124       istate->set_stack(stack->sp() - 1);
 125 
 126       // Restore the stack
 127       stack->set_sp(istate->stack_limit() + 1);
 128 
 129       // Resume the interpreter
 130       istate->set_msg(BytecodeInterpreter::method_resume);
 131     }
 132     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
 133       int monitor_words = frame::interpreter_frame_monitor_size();
 134 
 135       // Allocate the space
 136       stack->overflow_check(monitor_words, THREAD);
 137       if (HAS_PENDING_EXCEPTION)
 138         break;
 139       stack->alloc(monitor_words * wordSize);
 140 
 141       // Move the expression stack contents
 142       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
 143         *(p - monitor_words) = *p;
 144 
 145       // Move the expression stack pointers
 146       istate->set_stack_limit(istate->stack_limit() - monitor_words);
 147       istate->set_stack(istate->stack() - monitor_words);
 148       istate->set_stack_base(istate->stack_base() - monitor_words);
 149 
 150       // Zero the new monitor so the interpreter can find it.
 151       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
 152 
 153       // Resume the interpreter
 154       istate->set_msg(BytecodeInterpreter::got_monitors);
 155     }
 156     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
 157       // Copy the result into the caller's frame
 158       result_slots = type2size[result_type_of(method)];
 159       assert(result_slots >= 0 && result_slots <= 2, "what?");
 160       result = istate->stack() + result_slots;
 161       break;
 162     }
 163     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
 164       assert(HAS_PENDING_EXCEPTION, "should do");
 165       break;
 166     }
 167     else if (istate->msg() == BytecodeInterpreter::do_osr) {
 168       // Unwind the current frame
 169       thread->pop_zero_frame();
 170 
 171       // Remove any extension of the previous frame
 172       int extra_locals = method->max_locals() - method->size_of_parameters();
 173       stack->set_sp(stack->sp() + extra_locals);
 174 
 175       // Jump into the OSR method
 176       Interpreter::invoke_osr(
 177         method, istate->osr_entry(), istate->osr_buf(), THREAD);
 178       return;
 179     }
 180     else {
 181       ShouldNotReachHere();
 182     }
 183   }
 184 
 185   // Unwind the current frame
 186   thread->pop_zero_frame();
 187 
 188   // Pop our local variables
 189   stack->set_sp(stack->sp() + method->max_locals());
 190 
 191   // Push our result
 192   for (int i = 0; i < result_slots; i++)
 193     stack->push(result[-i]);
 194 }
 195 
 196 int CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 197   // Make sure method is native and not abstract
 198   assert(method->is_native() && !method->is_abstract(), "should be");
 199 
 200   JavaThread *thread = (JavaThread *) THREAD;
 201   ZeroStack *stack = thread->zero_stack();
 202 
 203   // Allocate and initialize our frame
 204   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
 205   thread->push_zero_frame(frame);
 206   interpreterState istate = frame->interpreter_state();
 207   intptr_t *locals = istate->locals();
 208 
 209   // Update the invocation counter
 210   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
 211     InvocationCounter *counter = method->invocation_counter();
 212     counter->increment();
 213     if (counter->reached_InvocationLimit()) {
 214       CALL_VM_NOCHECK(
 215         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
 216       if (HAS_PENDING_EXCEPTION)
 217         goto unwind_and_return;
 218     }
 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 unlock_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 unlock_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   stack->overflow_check(handler->argument_count() * 2, THREAD);
 270   if (HAS_PENDING_EXCEPTION)
 271     goto unlock_unwind_and_return;
 272 
 273   void **arguments;
 274   void *mirror; {
 275     arguments =
 276       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
 277     void **dst = arguments;
 278 
 279     void *env = thread->jni_environment();
 280     *(dst++) = &env;
 281 
 282     if (method->is_static()) {
 283       istate->set_oop_temp(
 284         method->constants()->pool_holder()->java_mirror());
 285       mirror = istate->oop_temp_addr();
 286       *(dst++) = &mirror;
 287     }
 288 
 289     intptr_t *src = locals;
 290     for (int i = dst - arguments; i < handler->argument_count(); i++) {
 291       ffi_type *type = handler->argument_type(i);
 292       if (type == &ffi_type_pointer) {
 293         if (*src) {
 294           stack->push((intptr_t) src);
 295           *(dst++) = stack->sp();
 296         }
 297         else {
 298           *(dst++) = src;
 299         }
 300         src--;
 301       }
 302       else if (type->size == 4) {
 303         *(dst++) = src--;
 304       }
 305       else if (type->size == 8) {
 306         src--;
 307         *(dst++) = src--;
 308       }
 309       else {
 310         ShouldNotReachHere();
 311       }
 312     }
 313   }
 314 
 315   // Set up the Java frame anchor
 316   thread->set_last_Java_frame();
 317 
 318   // Change the thread state to _thread_in_native
 319   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
 320 
 321   // Make the call
 322   intptr_t result[4 - LogBytesPerWord];
 323   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
 324 
 325   // Change the thread state back to _thread_in_Java.
 326   // ThreadStateTransition::transition_from_native() cannot be used
 327   // here because it does not check for asynchronous exceptions.
 328   // We have to manage the transition ourself.
 329   thread->set_thread_state(_thread_in_native_trans);
 330 
 331   // Make sure new state is visible in the GC thread
 332   if (os::is_MP()) {
 333     if (UseMembar) {
 334       OrderAccess::fence();
 335     }
 336     else {
 337       InterfaceSupport::serialize_memory(thread);
 338     }
 339   }
 340 
 341   // Handle safepoint operations, pending suspend requests,
 342   // and pending asynchronous exceptions.
 343   if (SafepointSynchronize::do_call_back() ||
 344       thread->has_special_condition_for_native_trans()) {
 345     JavaThread::check_special_condition_for_native_trans(thread);
 346     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
 347   }
 348 
 349   // Finally we can change the thread state to _thread_in_Java.
 350   thread->set_thread_state(_thread_in_Java);
 351   fixup_after_potential_safepoint();
 352 
 353   // Clear the frame anchor
 354   thread->reset_last_Java_frame();
 355 
 356   // If the result was an oop then unbox it and store it in
 357   // oop_temp where the garbage collector can see it before
 358   // we release the handle it might be protected by.
 359   if (handler->result_type() == &ffi_type_pointer) {
 360     if (result[0])
 361       istate->set_oop_temp(*(oop *) result[0]);
 362     else
 363       istate->set_oop_temp(NULL);
 364   }
 365 
 366   // Reset handle block
 367   thread->active_handles()->clear();
 368 
 369  unlock_unwind_and_return:
 370 
 371   // Unlock if necessary
 372   if (monitor) {
 373     BasicLock *lock = monitor->lock();
 374     markOop header = lock->displaced_header();
 375     oop rcvr = monitor->obj();
 376     monitor->set_obj(NULL);
 377 
 378     if (header != NULL) {
 379       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
 380         monitor->set_obj(rcvr); {
 381           HandleMark hm(thread);
 382           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
 383         }
 384       }
 385     }
 386   }
 387 
 388  unwind_and_return:
 389 
 390   // Unwind the current activation
 391   thread->pop_zero_frame();
 392 
 393   // Pop our parameters
 394   stack->set_sp(stack->sp() + method->size_of_parameters());
 395 
 396   // Push our result
 397   if (!HAS_PENDING_EXCEPTION) {
 398     BasicType type = result_type_of(method);
 399     stack->set_sp(stack->sp() - type2size[type]);
 400 
 401     switch (type) {
 402     case T_VOID:
 403       break;
 404 
 405     case T_BOOLEAN:
 406 #ifndef VM_LITTLE_ENDIAN
 407       result[0] <<= (BitsPerWord - BitsPerByte);
 408 #endif
 409       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
 410       break;
 411 
 412     case T_CHAR:
 413 #ifndef VM_LITTLE_ENDIAN
 414       result[0] <<= (BitsPerWord - BitsPerShort);
 415 #endif
 416       SET_LOCALS_INT(*(jchar *) result, 0);
 417       break;
 418 
 419     case T_BYTE:
 420 #ifndef VM_LITTLE_ENDIAN
 421       result[0] <<= (BitsPerWord - BitsPerByte);
 422 #endif
 423       SET_LOCALS_INT(*(jbyte *) result, 0);
 424       break;
 425 
 426     case T_SHORT:
 427 #ifndef VM_LITTLE_ENDIAN
 428       result[0] <<= (BitsPerWord - BitsPerShort);
 429 #endif
 430       SET_LOCALS_INT(*(jshort *) result, 0);
 431       break;
 432 
 433     case T_INT:
 434 #ifndef VM_LITTLE_ENDIAN
 435       result[0] <<= (BitsPerWord - BitsPerInt);
 436 #endif
 437       SET_LOCALS_INT(*(jint *) result, 0);
 438       break;
 439 
 440     case T_LONG:
 441       SET_LOCALS_LONG(*(jlong *) result, 0);
 442       break;
 443 
 444     case T_FLOAT:
 445       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
 446       break;
 447 
 448     case T_DOUBLE:
 449       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
 450       break;
 451 
 452     case T_OBJECT:
 453     case T_ARRAY:
 454       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
 455       break;
 456 
 457     default:
 458       ShouldNotReachHere();
 459     }
 460   }
 461 
 462   // No deoptimized frames on the stack
 463   return 0;
 464 }
 465 
 466 int CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 467   JavaThread *thread = (JavaThread *) THREAD;
 468   ZeroStack *stack = thread->zero_stack();
 469   intptr_t *locals = stack->sp();
 470 
 471   // Drop into the slow path if we need a safepoint check
 472   if (SafepointSynchronize::do_call_back()) {
 473     return normal_entry(method, 0, THREAD);
 474   }
 475 
 476   // Load the object pointer and drop into the slow path
 477   // if we have a NullPointerException
 478   oop object = LOCALS_OBJECT(0);
 479   if (object == NULL) {
 480     return normal_entry(method, 0, THREAD);
 481   }
 482 
 483   // Read the field index from the bytecode, which looks like this:
 484   //  0:  aload_0
 485   //  1:  getfield
 486   //  2:    index
 487   //  3:    index
 488   //  4:  ireturn/areturn
 489   // NB this is not raw bytecode: index is in machine order
 490   u1 *code = method->code_base();
 491   assert(code[0] == Bytecodes::_aload_0 &&
 492          code[1] == Bytecodes::_getfield &&
 493          (code[4] == Bytecodes::_ireturn ||
 494           code[4] == Bytecodes::_areturn), "should do");
 495   u2 index = Bytes::get_native_u2(&code[2]);
 496 
 497   // Get the entry from the constant pool cache, and drop into
 498   // the slow path if it has not been resolved
 499   constantPoolCacheOop cache = method->constants()->cache();
 500   ConstantPoolCacheEntry* entry = cache->entry_at(index);
 501   if (!entry->is_resolved(Bytecodes::_getfield)) {
 502     return normal_entry(method, 0, THREAD);
 503   }
 504 
 505   // Get the result and push it onto the stack
 506   switch (entry->flag_state()) {
 507   case ltos:
 508   case dtos:
 509     stack->overflow_check(1, CHECK_0);
 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   // No deoptimized frames on the stack
 591   return 0;
 592 }
 593 
 594 int CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 595   JavaThread *thread = (JavaThread *) THREAD;
 596   ZeroStack *stack = thread->zero_stack();
 597 
 598   // Drop into the slow path if we need a safepoint check
 599   if (SafepointSynchronize::do_call_back()) {
 600     return normal_entry(method, 0, THREAD);
 601   }
 602 
 603   // Pop our parameters
 604   stack->set_sp(stack->sp() + method->size_of_parameters());
 605 
 606   // No deoptimized frames on the stack
 607   return 0;
 608 }
 609 
 610 InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) {
 611   JavaThread *thread = (JavaThread *) THREAD;
 612   ZeroStack *stack = thread->zero_stack();
 613 
 614   // Calculate the size of the frame we'll build, including
 615   // any adjustments to the caller's frame that we'll make.
 616   int extra_locals  = 0;
 617   int monitor_words = 0;
 618   int stack_words   = 0;
 619 
 620   if (!method->is_native()) {
 621     extra_locals = method->max_locals() - method->size_of_parameters();
 622     stack_words  = method->max_stack();
 623   }
 624   if (method->is_synchronized()) {
 625     monitor_words = frame::interpreter_frame_monitor_size();
 626   }
 627   stack->overflow_check(
 628     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
 629 
 630   // Adjust the caller's stack frame to accomodate any additional
 631   // local variables we have contiguously with our parameters.
 632   for (int i = 0; i < extra_locals; i++)
 633     stack->push(0);
 634 
 635   intptr_t *locals;
 636   if (method->is_native())
 637     locals = stack->sp() + (method->size_of_parameters() - 1);
 638   else
 639     locals = stack->sp() + (method->max_locals() - 1);
 640 
 641   stack->push(0); // next_frame, filled in later
 642   intptr_t *fp = stack->sp();
 643   assert(fp - stack->sp() == next_frame_off, "should be");
 644 
 645   stack->push(INTERPRETER_FRAME);
 646   assert(fp - stack->sp() == frame_type_off, "should be");
 647 
 648   interpreterState istate =
 649     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 650   assert(fp - stack->sp() == istate_off, "should be");
 651 
 652   istate->set_locals(locals);
 653   istate->set_method(method);
 654   istate->set_self_link(istate);
 655   istate->set_prev_link(NULL);
 656   istate->set_thread(thread);
 657   istate->set_bcp(method->is_native() ? NULL : method->code_base());
 658   istate->set_constants(method->constants()->cache());
 659   istate->set_msg(BytecodeInterpreter::method_entry);
 660   istate->set_oop_temp(NULL);
 661   istate->set_mdx(NULL);
 662   istate->set_callee(NULL);
 663 
 664   istate->set_monitor_base((BasicObjectLock *) stack->sp());
 665   if (method->is_synchronized()) {
 666     BasicObjectLock *monitor =
 667       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
 668     oop object;
 669     if (method->is_static())
 670       object = method->constants()->pool_holder()->java_mirror();
 671     else
 672       object = (oop) locals[0];
 673     monitor->set_obj(object);
 674   }
 675 
 676   istate->set_stack_base(stack->sp());
 677   istate->set_stack(stack->sp() - 1);
 678   if (stack_words)
 679     stack->alloc(stack_words * wordSize);
 680   istate->set_stack_limit(stack->sp() - 1);
 681 
 682   return (InterpreterFrame *) fp;
 683 }
 684 
 685 int AbstractInterpreter::BasicType_as_index(BasicType type) {
 686   int i = 0;
 687   switch (type) {
 688     case T_BOOLEAN: i = 0; break;
 689     case T_CHAR   : i = 1; break;
 690     case T_BYTE   : i = 2; break;
 691     case T_SHORT  : i = 3; break;
 692     case T_INT    : i = 4; break;
 693     case T_LONG   : i = 5; break;
 694     case T_VOID   : i = 6; break;
 695     case T_FLOAT  : i = 7; break;
 696     case T_DOUBLE : i = 8; break;
 697     case T_OBJECT : i = 9; break;
 698     case T_ARRAY  : i = 9; break;
 699     default       : ShouldNotReachHere();
 700   }
 701   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
 702          "index out of bounds");
 703   return i;
 704 }
 705 
 706 BasicType CppInterpreter::result_type_of(methodOop method) {
 707   BasicType t;
 708   switch (method->result_index()) {
 709     case 0 : t = T_BOOLEAN; break;
 710     case 1 : t = T_CHAR;    break;
 711     case 2 : t = T_BYTE;    break;
 712     case 3 : t = T_SHORT;   break;
 713     case 4 : t = T_INT;     break;
 714     case 5 : t = T_LONG;    break;
 715     case 6 : t = T_VOID;    break;
 716     case 7 : t = T_FLOAT;   break;
 717     case 8 : t = T_DOUBLE;  break;
 718     case 9 : t = T_OBJECT;  break;
 719     default: ShouldNotReachHere();
 720   }
 721   assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
 722          "out of step with AbstractInterpreter::BasicType_as_index");
 723   return t;
 724 }
 725 
 726 address InterpreterGenerator::generate_empty_entry() {
 727   if (!UseFastEmptyMethods)
 728     return NULL;
 729 
 730   return generate_entry((address) CppInterpreter::empty_entry);
 731 }
 732 
 733 address InterpreterGenerator::generate_accessor_entry() {
 734   if (!UseFastAccessorMethods)
 735     return NULL;
 736 
 737   return generate_entry((address) CppInterpreter::accessor_entry);
 738 }
 739 
 740 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 741   assert(synchronized == false, "should be");
 742 
 743   return generate_entry((address) CppInterpreter::native_entry);
 744 }
 745 
 746 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
 747   assert(synchronized == false, "should be");
 748 
 749   return generate_entry((address) CppInterpreter::normal_entry);
 750 }
 751 
 752 address AbstractInterpreterGenerator::generate_method_entry(
 753     AbstractInterpreter::MethodKind kind) {
 754   address entry_point = NULL;
 755 
 756   switch (kind) {
 757   case Interpreter::zerolocals:
 758   case Interpreter::zerolocals_synchronized:
 759     break;
 760 
 761   case Interpreter::native:
 762     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
 763     break;
 764 
 765   case Interpreter::native_synchronized:
 766     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
 767     break;
 768 
 769   case Interpreter::empty:
 770     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
 771     break;
 772 
 773   case Interpreter::accessor:
 774     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
 775     break;
 776 
 777   case Interpreter::abstract:
 778     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
 779     break;
 780 
 781   case Interpreter::method_handle:
 782     entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
 783     break;
 784 
 785   case Interpreter::java_lang_math_sin:
 786   case Interpreter::java_lang_math_cos:
 787   case Interpreter::java_lang_math_tan:
 788   case Interpreter::java_lang_math_abs:
 789   case Interpreter::java_lang_math_log:
 790   case Interpreter::java_lang_math_log10:
 791   case Interpreter::java_lang_math_sqrt:
 792     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
 793     break;
 794 
 795   default:
 796     ShouldNotReachHere();
 797   }
 798 
 799   if (entry_point == NULL)
 800     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
 801 
 802   return entry_point;
 803 }
 804 
 805 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
 806  : CppInterpreterGenerator(code) {
 807    generate_all();
 808 }
 809 
 810 // Deoptimization helpers
 811 
 812 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
 813   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 814 
 815   int size_in_words = size >> LogBytesPerWord;
 816   assert(size_in_words * wordSize == size, "unaligned");
 817   assert(size_in_words >= header_words, "too small");
 818   stack->overflow_check(size_in_words, CHECK_NULL);
 819 
 820   stack->push(0); // next_frame, filled in later
 821   intptr_t *fp = stack->sp();
 822   assert(fp - stack->sp() == next_frame_off, "should be");
 823 
 824   stack->push(INTERPRETER_FRAME);
 825   assert(fp - stack->sp() == frame_type_off, "should be");
 826 
 827   interpreterState istate =
 828     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 829   assert(fp - stack->sp() == istate_off, "should be");
 830   istate->set_self_link(NULL); // mark invalid
 831 
 832   stack->alloc((size_in_words - header_words) * wordSize);
 833 
 834   return (InterpreterFrame *) fp;
 835 }
 836 
 837 int AbstractInterpreter::layout_activation(methodOop method,
 838                                            int       tempcount,
 839                                            int       popframe_extra_args,
 840                                            int       moncount,
 841                                            int       callee_param_count,
 842                                            int       callee_locals,
 843                                            frame*    caller,
 844                                            frame*    interpreter_frame,
 845                                            bool      is_top_frame) {
 846   assert(popframe_extra_args == 0, "what to do?");
 847   assert(!is_top_frame || (!callee_locals && !callee_param_count),
 848          "top frame should have no caller");
 849 
 850   // This code must exactly match what InterpreterFrame::build
 851   // does (the full InterpreterFrame::build, that is, not the
 852   // one that creates empty frames for the deoptimizer).
 853   //
 854   // If interpreter_frame is not NULL then it will be filled in.
 855   // It's size is determined by a previous call to this method,
 856   // so it should be correct.
 857   //
 858   // Note that tempcount is the current size of the expression
 859   // stack.  For top most frames we will allocate a full sized
 860   // expression stack and not the trimmed version that non-top
 861   // frames have.
 862 
 863   int header_words        = InterpreterFrame::header_words;
 864   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
 865   int stack_words         = is_top_frame ? method->max_stack() : tempcount;
 866   int callee_extra_locals = callee_locals - callee_param_count;
 867 
 868   if (interpreter_frame) {
 869     intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
 870     interpreterState istate = interpreter_frame->get_interpreterState();
 871     intptr_t *monitor_base  = (intptr_t*) istate;
 872     intptr_t *stack_base    = monitor_base - monitor_words;
 873     intptr_t *stack         = stack_base - tempcount - 1;
 874 
 875     BytecodeInterpreter::layout_interpreterState(istate,
 876                                                  caller,
 877                                                  NULL,
 878                                                  method,
 879                                                  locals,
 880                                                  stack,
 881                                                  stack_base,
 882                                                  monitor_base,
 883                                                  NULL,
 884                                                  is_top_frame);
 885   }
 886   return header_words + monitor_words + stack_words + callee_extra_locals;
 887 }
 888 
 889 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
 890                                                   frame*    caller,
 891                                                   frame*    current,
 892                                                   methodOop method,
 893                                                   intptr_t* locals,
 894                                                   intptr_t* stack,
 895                                                   intptr_t* stack_base,
 896                                                   intptr_t* monitor_base,
 897                                                   intptr_t* frame_bottom,
 898                                                   bool      is_top_frame) {
 899   istate->set_locals(locals);
 900   istate->set_method(method);
 901   istate->set_self_link(istate);
 902   istate->set_prev_link(NULL);
 903   // thread will be set by a hacky repurposing of frame::patch_pc()
 904   // bcp will be set by vframeArrayElement::unpack_on_stack()
 905   istate->set_constants(method->constants()->cache());
 906   istate->set_msg(BytecodeInterpreter::method_resume);
 907   istate->set_bcp_advance(0);
 908   istate->set_oop_temp(NULL);
 909   istate->set_mdx(NULL);
 910   if (caller->is_interpreted_frame()) {
 911     interpreterState prev = caller->get_interpreterState();
 912     prev->set_callee(method);
 913     if (*prev->bcp() == Bytecodes::_invokeinterface)
 914       prev->set_bcp_advance(5);
 915     else
 916       prev->set_bcp_advance(3);
 917   }
 918   istate->set_callee(NULL);
 919   istate->set_monitor_base((BasicObjectLock *) monitor_base);
 920   istate->set_stack_base(stack_base);
 921   istate->set_stack(stack);
 922   istate->set_stack_limit(stack_base - method->max_stack() - 1);
 923 }
 924 
 925 address CppInterpreter::return_entry(TosState state, int length) {
 926   ShouldNotCallThis();
 927 }
 928 
 929 address CppInterpreter::deopt_entry(TosState state, int length) {
 930   return NULL;
 931 }
 932 
 933 // Helper for (runtime) stack overflow checks
 934 
 935 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
 936   return 0;
 937 }
 938 
 939 // Helper for figuring out if frames are interpreter frames
 940 
 941 bool CppInterpreter::contains(address pc) {
 942 #ifdef PRODUCT
 943   ShouldNotCallThis();
 944 #else
 945   return false; // make frame::print_value_on work
 946 #endif // !PRODUCT
 947 }
 948 
 949 // Result handlers and convertors
 950 
 951 address CppInterpreterGenerator::generate_result_handler_for(
 952     BasicType type) {
 953   assembler()->advance(1);
 954   return ShouldNotCallThisStub();
 955 }
 956 
 957 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
 958     BasicType type) {
 959   assembler()->advance(1);
 960   return ShouldNotCallThisStub();
 961 }
 962 
 963 address CppInterpreterGenerator::generate_stack_to_stack_converter(
 964     BasicType type) {
 965   assembler()->advance(1);
 966   return ShouldNotCallThisStub();
 967 }
 968 
 969 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
 970     BasicType type) {
 971   assembler()->advance(1);
 972   return ShouldNotCallThisStub();
 973 }
 974 
 975 #endif // CC_INTERP