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