1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  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_NOFIX(func)             \
  60   thread->set_last_Java_frame();                \
  61   func;                                         \
  62   thread->reset_last_Java_frame();
  63 
  64 #define CALL_VM_NOCHECK(func)                   \
  65   CALL_VM_NOCHECK_NOFIX(func)                   \
  66   fixup_after_potential_safepoint()
  67 
  68 int CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
  69   JavaThread *thread = (JavaThread *) THREAD;
  70 
  71   // Allocate and initialize our frame.
  72   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
  73   thread->push_zero_frame(frame);
  74 
  75   // Execute those bytecodes!
  76   main_loop(0, THREAD);
  77 
  78   // No deoptimized frames on the stack
  79   return 0;
  80 }
  81 
  82 void CppInterpreter::main_loop(int recurse, TRAPS) {
  83   JavaThread *thread = (JavaThread *) THREAD;
  84   ZeroStack *stack = thread->zero_stack();
  85 
  86   // If we are entering from a deopt we may need to call
  87   // ourself a few times in order to get to our frame.
  88   if (recurse)
  89     main_loop(recurse - 1, THREAD);
  90 
  91   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  92   interpreterState istate = frame->interpreter_state();
  93   methodOop method = istate->method();
  94 
  95   intptr_t *result = NULL;
  96   int result_slots = 0;
  97 
  98   while (true) {
  99     // We can set up the frame anchor with everything we want at
 100     // this point as we are thread_in_Java and no safepoints can
 101     // occur until we go to vm mode.  We do have to clear flags
 102     // on return from vm but that is it.
 103     thread->set_last_Java_frame();
 104 
 105     // Call the interpreter
 106     if (JvmtiExport::can_post_interpreter_events())
 107       BytecodeInterpreter::runWithChecks(istate);
 108     else
 109       BytecodeInterpreter::run(istate);
 110     fixup_after_potential_safepoint();
 111 
 112     // Clear the frame anchor
 113     thread->reset_last_Java_frame();
 114 
 115     // Examine the message from the interpreter to decide what to do
 116     if (istate->msg() == BytecodeInterpreter::call_method) {
 117       methodOop callee = istate->callee();
 118 
 119       // Trim back the stack to put the parameters at the top
 120       stack->set_sp(istate->stack() + 1);
 121 
 122       // Make the call
 123       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
 124       fixup_after_potential_safepoint();
 125 
 126       // Convert the result
 127       istate->set_stack(stack->sp() - 1);
 128 
 129       // Restore the stack
 130       stack->set_sp(istate->stack_limit() + 1);
 131 
 132       // Resume the interpreter
 133       istate->set_msg(BytecodeInterpreter::method_resume);
 134     }
 135     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
 136       int monitor_words = frame::interpreter_frame_monitor_size();
 137 
 138       // Allocate the space
 139       stack->overflow_check(monitor_words, THREAD);
 140       if (HAS_PENDING_EXCEPTION)
 141         break;
 142       stack->alloc(monitor_words * wordSize);
 143 
 144       // Move the expression stack contents
 145       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
 146         *(p - monitor_words) = *p;
 147 
 148       // Move the expression stack pointers
 149       istate->set_stack_limit(istate->stack_limit() - monitor_words);
 150       istate->set_stack(istate->stack() - monitor_words);
 151       istate->set_stack_base(istate->stack_base() - monitor_words);
 152 
 153       // Zero the new monitor so the interpreter can find it.
 154       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
 155 
 156       // Resume the interpreter
 157       istate->set_msg(BytecodeInterpreter::got_monitors);
 158     }
 159     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
 160       // Copy the result into the caller's frame
 161       result_slots = type2size[result_type_of(method)];
 162       assert(result_slots >= 0 && result_slots <= 2, "what?");
 163       result = istate->stack() + result_slots;
 164       break;
 165     }
 166     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
 167       assert(HAS_PENDING_EXCEPTION, "should do");
 168       break;
 169     }
 170     else if (istate->msg() == BytecodeInterpreter::do_osr) {
 171       // Unwind the current frame
 172       thread->pop_zero_frame();
 173 
 174       // Remove any extension of the previous frame
 175       int extra_locals = method->max_locals() - method->size_of_parameters();
 176       stack->set_sp(stack->sp() + extra_locals);
 177 
 178       // Jump into the OSR method
 179       Interpreter::invoke_osr(
 180         method, istate->osr_entry(), istate->osr_buf(), THREAD);
 181       return;
 182     }
 183     else if (istate->msg() == BytecodeInterpreter::call_method_handle) {
 184       oop method_handle = istate->callee();
 185 
 186       // Trim back the stack to put the parameters at the top
 187       stack->set_sp(istate->stack() + 1);
 188 
 189       // Make the call
 190       process_method_handle(method_handle, THREAD);
 191       fixup_after_potential_safepoint();
 192 
 193       // Convert the result
 194       istate->set_stack(stack->sp() - 1);
 195 
 196       // Restore the stack
 197       stack->set_sp(istate->stack_limit() + 1);
 198 
 199       // Resume the interpreter
 200       istate->set_msg(BytecodeInterpreter::method_resume);
 201     }
 202     else {
 203       ShouldNotReachHere();
 204     }
 205   }
 206 
 207   // Unwind the current frame
 208   thread->pop_zero_frame();
 209 
 210   // Pop our local variables
 211   stack->set_sp(stack->sp() + method->max_locals());
 212 
 213   // Push our result
 214   for (int i = 0; i < result_slots; i++)
 215     stack->push(result[-i]);
 216 }
 217 
 218 int CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 219   // Make sure method is native and not abstract
 220   assert(method->is_native() && !method->is_abstract(), "should be");
 221 
 222   JavaThread *thread = (JavaThread *) THREAD;
 223   ZeroStack *stack = thread->zero_stack();
 224 
 225   // Allocate and initialize our frame
 226   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
 227   thread->push_zero_frame(frame);
 228   interpreterState istate = frame->interpreter_state();
 229   intptr_t *locals = istate->locals();
 230 
 231   // Update the invocation counter
 232   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
 233     InvocationCounter *counter = method->invocation_counter();
 234     counter->increment();
 235     if (counter->reached_InvocationLimit()) {
 236       CALL_VM_NOCHECK(
 237         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
 238       if (HAS_PENDING_EXCEPTION)
 239         goto unwind_and_return;
 240     }
 241   }
 242 
 243   // Lock if necessary
 244   BasicObjectLock *monitor;
 245   monitor = NULL;
 246   if (method->is_synchronized()) {
 247     monitor = (BasicObjectLock*) istate->stack_base();
 248     oop lockee = monitor->obj();
 249     markOop disp = lockee->mark()->set_unlocked();
 250 
 251     monitor->lock()->set_displaced_header(disp);
 252     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
 253       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
 254         monitor->lock()->set_displaced_header(NULL);
 255       }
 256       else {
 257         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
 258         if (HAS_PENDING_EXCEPTION)
 259           goto unwind_and_return;
 260       }
 261     }
 262   }
 263 
 264   // Get the signature handler
 265   InterpreterRuntime::SignatureHandler *handler; {
 266     address handlerAddr = method->signature_handler();
 267     if (handlerAddr == NULL) {
 268       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
 269       if (HAS_PENDING_EXCEPTION)
 270         goto unlock_unwind_and_return;
 271 
 272       handlerAddr = method->signature_handler();
 273       assert(handlerAddr != NULL, "eh?");
 274     }
 275     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
 276       CALL_VM_NOCHECK(handlerAddr =
 277         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
 278       if (HAS_PENDING_EXCEPTION)
 279         goto unlock_unwind_and_return;
 280     }
 281     handler = \
 282       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
 283   }
 284 
 285   // Get the native function entry point
 286   address function;
 287   function = method->native_function();
 288   assert(function != NULL, "should be set if signature handler is");
 289 
 290   // Build the argument list
 291   stack->overflow_check(handler->argument_count() * 2, THREAD);
 292   if (HAS_PENDING_EXCEPTION)
 293     goto unlock_unwind_and_return;
 294 
 295   void **arguments;
 296   void *mirror; {
 297     arguments =
 298       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
 299     void **dst = arguments;
 300 
 301     void *env = thread->jni_environment();
 302     *(dst++) = &env;
 303 
 304     if (method->is_static()) {
 305       istate->set_oop_temp(
 306         method->constants()->pool_holder()->java_mirror());
 307       mirror = istate->oop_temp_addr();
 308       *(dst++) = &mirror;
 309     }
 310 
 311     intptr_t *src = locals;
 312     for (int i = dst - arguments; i < handler->argument_count(); i++) {
 313       ffi_type *type = handler->argument_type(i);
 314       if (type == &ffi_type_pointer) {
 315         if (*src) {
 316           stack->push((intptr_t) src);
 317           *(dst++) = stack->sp();
 318         }
 319         else {
 320           *(dst++) = src;
 321         }
 322         src--;
 323       }
 324       else if (type->size == 4) {
 325         *(dst++) = src--;
 326       }
 327       else if (type->size == 8) {
 328         src--;
 329         *(dst++) = src--;
 330       }
 331       else {
 332         ShouldNotReachHere();
 333       }
 334     }
 335   }
 336 
 337   // Set up the Java frame anchor
 338   thread->set_last_Java_frame();
 339 
 340   // Change the thread state to _thread_in_native
 341   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
 342 
 343   // Make the call
 344   intptr_t result[4 - LogBytesPerWord];
 345   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
 346 
 347   // Change the thread state back to _thread_in_Java.
 348   // ThreadStateTransition::transition_from_native() cannot be used
 349   // here because it does not check for asynchronous exceptions.
 350   // We have to manage the transition ourself.
 351   thread->set_thread_state(_thread_in_native_trans);
 352 
 353   // Make sure new state is visible in the GC thread
 354   if (os::is_MP()) {
 355     if (UseMembar) {
 356       OrderAccess::fence();
 357     }
 358     else {
 359       InterfaceSupport::serialize_memory(thread);
 360     }
 361   }
 362 
 363   // Handle safepoint operations, pending suspend requests,
 364   // and pending asynchronous exceptions.
 365   if (SafepointSynchronize::do_call_back() ||
 366       thread->has_special_condition_for_native_trans()) {
 367     JavaThread::check_special_condition_for_native_trans(thread);
 368     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
 369   }
 370 
 371   // Finally we can change the thread state to _thread_in_Java.
 372   thread->set_thread_state(_thread_in_Java);
 373   fixup_after_potential_safepoint();
 374 
 375   // Clear the frame anchor
 376   thread->reset_last_Java_frame();
 377 
 378   // If the result was an oop then unbox it and store it in
 379   // oop_temp where the garbage collector can see it before
 380   // we release the handle it might be protected by.
 381   if (handler->result_type() == &ffi_type_pointer) {
 382     if (result[0])
 383       istate->set_oop_temp(*(oop *) result[0]);
 384     else
 385       istate->set_oop_temp(NULL);
 386   }
 387 
 388   // Reset handle block
 389   thread->active_handles()->clear();
 390 
 391  unlock_unwind_and_return:
 392 
 393   // Unlock if necessary
 394   if (monitor) {
 395     BasicLock *lock = monitor->lock();
 396     markOop header = lock->displaced_header();
 397     oop rcvr = monitor->obj();
 398     monitor->set_obj(NULL);
 399 
 400     if (header != NULL) {
 401       if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
 402         monitor->set_obj(rcvr); {
 403           HandleMark hm(thread);
 404           CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
 405         }
 406       }
 407     }
 408   }
 409 
 410  unwind_and_return:
 411 
 412   // Unwind the current activation
 413   thread->pop_zero_frame();
 414 
 415   // Pop our parameters
 416   stack->set_sp(stack->sp() + method->size_of_parameters());
 417 
 418   // Push our result
 419   if (!HAS_PENDING_EXCEPTION) {
 420     BasicType type = result_type_of(method);
 421     stack->set_sp(stack->sp() - type2size[type]);
 422 
 423     switch (type) {
 424     case T_VOID:
 425       break;
 426 
 427     case T_BOOLEAN:
 428 #ifndef VM_LITTLE_ENDIAN
 429       result[0] <<= (BitsPerWord - BitsPerByte);
 430 #endif
 431       SET_LOCALS_INT(*(jboolean *) result != 0, 0);
 432       break;
 433 
 434     case T_CHAR:
 435 #ifndef VM_LITTLE_ENDIAN
 436       result[0] <<= (BitsPerWord - BitsPerShort);
 437 #endif
 438       SET_LOCALS_INT(*(jchar *) result, 0);
 439       break;
 440 
 441     case T_BYTE:
 442 #ifndef VM_LITTLE_ENDIAN
 443       result[0] <<= (BitsPerWord - BitsPerByte);
 444 #endif
 445       SET_LOCALS_INT(*(jbyte *) result, 0);
 446       break;
 447 
 448     case T_SHORT:
 449 #ifndef VM_LITTLE_ENDIAN
 450       result[0] <<= (BitsPerWord - BitsPerShort);
 451 #endif
 452       SET_LOCALS_INT(*(jshort *) result, 0);
 453       break;
 454 
 455     case T_INT:
 456 #ifndef VM_LITTLE_ENDIAN
 457       result[0] <<= (BitsPerWord - BitsPerInt);
 458 #endif
 459       SET_LOCALS_INT(*(jint *) result, 0);
 460       break;
 461 
 462     case T_LONG:
 463       SET_LOCALS_LONG(*(jlong *) result, 0);
 464       break;
 465 
 466     case T_FLOAT:
 467       SET_LOCALS_FLOAT(*(jfloat *) result, 0);
 468       break;
 469 
 470     case T_DOUBLE:
 471       SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
 472       break;
 473 
 474     case T_OBJECT:
 475     case T_ARRAY:
 476       SET_LOCALS_OBJECT(istate->oop_temp(), 0);
 477       break;
 478 
 479     default:
 480       ShouldNotReachHere();
 481     }
 482   }
 483 
 484   // No deoptimized frames on the stack
 485   return 0;
 486 }
 487 
 488 int CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 489   JavaThread *thread = (JavaThread *) THREAD;
 490   ZeroStack *stack = thread->zero_stack();
 491   intptr_t *locals = stack->sp();
 492 
 493   // Drop into the slow path if we need a safepoint check
 494   if (SafepointSynchronize::do_call_back()) {
 495     return normal_entry(method, 0, THREAD);
 496   }
 497 
 498   // Load the object pointer and drop into the slow path
 499   // if we have a NullPointerException
 500   oop object = LOCALS_OBJECT(0);
 501   if (object == NULL) {
 502     return normal_entry(method, 0, THREAD);
 503   }
 504 
 505   // Read the field index from the bytecode, which looks like this:
 506   //  0:  aload_0
 507   //  1:  getfield
 508   //  2:    index
 509   //  3:    index
 510   //  4:  ireturn/areturn
 511   // NB this is not raw bytecode: index is in machine order
 512   u1 *code = method->code_base();
 513   assert(code[0] == Bytecodes::_aload_0 &&
 514          code[1] == Bytecodes::_getfield &&
 515          (code[4] == Bytecodes::_ireturn ||
 516           code[4] == Bytecodes::_areturn), "should do");
 517   u2 index = Bytes::get_native_u2(&code[2]);
 518 
 519   // Get the entry from the constant pool cache, and drop into
 520   // the slow path if it has not been resolved
 521   constantPoolCacheOop cache = method->constants()->cache();
 522   ConstantPoolCacheEntry* entry = cache->entry_at(index);
 523   if (!entry->is_resolved(Bytecodes::_getfield)) {
 524     return normal_entry(method, 0, THREAD);
 525   }
 526 
 527   // Get the result and push it onto the stack
 528   switch (entry->flag_state()) {
 529   case ltos:
 530   case dtos:
 531     stack->overflow_check(1, CHECK_0);
 532     stack->alloc(wordSize);
 533     break;
 534   }
 535   if (entry->is_volatile()) {
 536     switch (entry->flag_state()) {
 537     case ctos:
 538       SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
 539       break;
 540 
 541     case btos:
 542       SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
 543       break;
 544 
 545     case stos:
 546       SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
 547       break;
 548 
 549     case itos:
 550       SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
 551       break;
 552 
 553     case ltos:
 554       SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
 555       break;
 556 
 557     case ftos:
 558       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
 559       break;
 560 
 561     case dtos:
 562       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
 563       break;
 564 
 565     case atos:
 566       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
 567       break;
 568 
 569     default:
 570       ShouldNotReachHere();
 571     }
 572   }
 573   else {
 574     switch (entry->flag_state()) {
 575     case ctos:
 576       SET_LOCALS_INT(object->char_field(entry->f2()), 0);
 577       break;
 578 
 579     case btos:
 580       SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
 581       break;
 582 
 583     case stos:
 584       SET_LOCALS_INT(object->short_field(entry->f2()), 0);
 585       break;
 586 
 587     case itos:
 588       SET_LOCALS_INT(object->int_field(entry->f2()), 0);
 589       break;
 590 
 591     case ltos:
 592       SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
 593       break;
 594 
 595     case ftos:
 596       SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
 597       break;
 598 
 599     case dtos:
 600       SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
 601       break;
 602 
 603     case atos:
 604       SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
 605       break;
 606 
 607     default:
 608       ShouldNotReachHere();
 609     }
 610   }
 611 
 612   // No deoptimized frames on the stack
 613   return 0;
 614 }
 615 
 616 int CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
 617   JavaThread *thread = (JavaThread *) THREAD;
 618   ZeroStack *stack = thread->zero_stack();
 619 
 620   // Drop into the slow path if we need a safepoint check
 621   if (SafepointSynchronize::do_call_back()) {
 622     return normal_entry(method, 0, THREAD);
 623   }
 624 
 625   // Pop our parameters
 626   stack->set_sp(stack->sp() + method->size_of_parameters());
 627 
 628   // No deoptimized frames on the stack
 629   return 0;
 630 }
 631 
 632 int CppInterpreter::method_handle_entry(methodOop method,
 633                                         intptr_t UNUSED, TRAPS) {
 634   JavaThread *thread = (JavaThread *) THREAD;
 635   ZeroStack *stack = thread->zero_stack();
 636   int argument_slots = method->size_of_parameters();
 637   int result_slots = type2size[result_type_of(method)];
 638   intptr_t *vmslots = stack->sp();
 639   intptr_t *unwind_sp = vmslots + argument_slots;
 640 
 641   // Find the MethodType
 642   address p = (address) method;
 643   for (jint* pc = method->method_type_offsets_chain(); (*pc) != -1; pc++) {
 644     p = *(address*)(p + (*pc));
 645   }
 646   oop method_type = (oop) p;
 647 
 648   // The MethodHandle is in the slot after the arguments
 649   oop form = java_lang_invoke_MethodType::form(method_type);
 650   int num_vmslots = java_lang_invoke_MethodTypeForm::vmslots(form);
 651   assert(argument_slots == num_vmslots + 1, "should be");
 652   oop method_handle = VMSLOTS_OBJECT(num_vmslots);
 653 
 654   // InvokeGeneric requires some extra shuffling
 655   oop mhtype = java_lang_invoke_MethodHandle::type(method_handle);
 656   bool is_exact = mhtype == method_type;
 657   if (!is_exact) {
 658     if (method->intrinsic_id() == vmIntrinsics::_invokeExact) {
 659       CALL_VM_NOCHECK_NOFIX(
 660         InterpreterRuntime::throw_WrongMethodTypeException(
 661           thread, method_type, mhtype));
 662       // NB all oops trashed!
 663       assert(HAS_PENDING_EXCEPTION, "should do");
 664       stack->set_sp(unwind_sp);
 665       return 0;
 666     }
 667     assert(method->intrinsic_id() == vmIntrinsics::_invokeGeneric, "should be");
 668 
 669     // Load up an adapter from the calling type
 670     // NB the x86 code for this (in methodHandles_x86.cpp, search for
 671     // "genericInvoker") is really really odd.  I'm hoping it's trying
 672     // to accomodate odd VM/class library combinations I can ignore.
 673     oop adapter = java_lang_invoke_MethodTypeForm::genericInvoker(form);
 674     if (adapter == NULL) {
 675       CALL_VM_NOCHECK_NOFIX(
 676         InterpreterRuntime::throw_WrongMethodTypeException(
 677           thread, method_type, mhtype));
 678       // NB all oops trashed!
 679       assert(HAS_PENDING_EXCEPTION, "should do");
 680       stack->set_sp(unwind_sp);
 681       return 0;
 682     }
 683 
 684     // Adapters are shared among form-families of method-type.  The
 685     // type being called is passed as a trusted first argument so that
 686     // the adapter knows the actual types of its arguments and return
 687     // values.
 688     insert_vmslots(num_vmslots + 1, 1, THREAD);
 689     if (HAS_PENDING_EXCEPTION) {
 690       // NB all oops trashed!
 691       stack->set_sp(unwind_sp);
 692       return 0;
 693     }
 694 
 695     vmslots = stack->sp();
 696     num_vmslots++;
 697     SET_VMSLOTS_OBJECT(method_type, num_vmslots);
 698 
 699     method_handle = adapter;
 700   }
 701 
 702   // Start processing
 703   process_method_handle(method_handle, THREAD);
 704   if (HAS_PENDING_EXCEPTION)
 705     result_slots = 0;
 706 
 707   // If this is an invokeExact then the eventual callee will not
 708   // have unwound the method handle argument so we have to do it.
 709   // If a result is being returned the it will be above the method
 710   // handle argument we're unwinding.
 711   if (is_exact) {
 712     intptr_t result[2];
 713     for (int i = 0; i < result_slots; i++)
 714       result[i] = stack->pop();
 715     stack->pop();
 716     for (int i = result_slots - 1; i >= 0; i--)
 717       stack->push(result[i]);
 718   }
 719 
 720   // Check
 721   assert(stack->sp() == unwind_sp - result_slots, "should be");
 722 
 723   // No deoptimized frames on the stack
 724   return 0;
 725 }
 726 
 727 void CppInterpreter::process_method_handle(oop method_handle, TRAPS) {
 728   JavaThread *thread = (JavaThread *) THREAD;
 729   ZeroStack *stack = thread->zero_stack();
 730   intptr_t *vmslots = stack->sp();
 731 
 732   bool direct_to_method = false;
 733   BasicType src_rtype = T_ILLEGAL;
 734   BasicType dst_rtype = T_ILLEGAL;
 735 
 736   MethodHandleEntry *entry =
 737     java_lang_invoke_MethodHandle::vmentry(method_handle);
 738   MethodHandles::EntryKind entry_kind =
 739     (MethodHandles::EntryKind) (((intptr_t) entry) & 0xffffffff);
 740 
 741   methodOop method = NULL;
 742   switch (entry_kind) {
 743   case MethodHandles::_invokestatic_mh:
 744     direct_to_method = true;
 745     break;
 746 
 747   case MethodHandles::_invokespecial_mh:
 748   case MethodHandles::_invokevirtual_mh:
 749   case MethodHandles::_invokeinterface_mh:
 750     {
 751       oop receiver =
 752         VMSLOTS_OBJECT(
 753           java_lang_invoke_MethodHandle::vmslots(method_handle) - 1);
 754       if (receiver == NULL) {
 755           stack->set_sp(calculate_unwind_sp(stack, method_handle));
 756           CALL_VM_NOCHECK_NOFIX(
 757             throw_exception(
 758               thread, vmSymbols::java_lang_NullPointerException()));
 759           // NB all oops trashed!
 760           assert(HAS_PENDING_EXCEPTION, "should do");
 761           return;
 762       }
 763       if (entry_kind != MethodHandles::_invokespecial_mh) {
 764         int index = java_lang_invoke_DirectMethodHandle::vmindex(method_handle);
 765         instanceKlass* rcvrKlass =
 766           (instanceKlass *) receiver->klass()->klass_part();
 767         if (entry_kind == MethodHandles::_invokevirtual_mh) {
 768           method = (methodOop) rcvrKlass->start_of_vtable()[index];
 769         }
 770         else {
 771           oop iclass = java_lang_invoke_MethodHandle::vmtarget(method_handle);
 772           itableOffsetEntry* ki =
 773             (itableOffsetEntry *) rcvrKlass->start_of_itable();
 774           int i, length = rcvrKlass->itable_length();
 775           for (i = 0; i < length; i++, ki++ ) {
 776             if (ki->interface_klass() == iclass)
 777               break;
 778           }
 779           if (i == length) {
 780             stack->set_sp(calculate_unwind_sp(stack, method_handle));
 781             CALL_VM_NOCHECK_NOFIX(
 782               throw_exception(
 783                 thread, vmSymbols::java_lang_IncompatibleClassChangeError()));
 784             // NB all oops trashed!
 785             assert(HAS_PENDING_EXCEPTION, "should do");
 786             return;
 787           }
 788           itableMethodEntry* im = ki->first_method_entry(receiver->klass());
 789           method = im[index].method();
 790           if (method == NULL) {
 791             stack->set_sp(calculate_unwind_sp(stack, method_handle));
 792             CALL_VM_NOCHECK_NOFIX(
 793               throw_exception(
 794                 thread, vmSymbols::java_lang_AbstractMethodError()));
 795             // NB all oops trashed!
 796             assert(HAS_PENDING_EXCEPTION, "should do");
 797             return;
 798           }
 799         }
 800       }
 801     }
 802     direct_to_method = true;
 803     break;
 804 
 805   case MethodHandles::_bound_ref_direct_mh:
 806   case MethodHandles::_bound_int_direct_mh:
 807   case MethodHandles::_bound_long_direct_mh:
 808     direct_to_method = true;
 809     // fall through
 810   case MethodHandles::_bound_ref_mh:
 811   case MethodHandles::_bound_int_mh:
 812   case MethodHandles::_bound_long_mh:
 813     {
 814       BasicType arg_type  = T_ILLEGAL;
 815       int       arg_mask  = -1;
 816       int       arg_slots = -1;
 817       MethodHandles::get_ek_bound_mh_info(
 818         entry_kind, arg_type, arg_mask, arg_slots);
 819       int arg_slot =
 820         java_lang_invoke_BoundMethodHandle::vmargslot(method_handle);
 821 
 822       // Create the new slot(s)
 823       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
 824       insert_vmslots(arg_slot, arg_slots, THREAD);
 825       if (HAS_PENDING_EXCEPTION) {
 826         // all oops trashed
 827         stack->set_sp(unwind_sp);
 828         return;
 829       }
 830       vmslots = stack->sp();
 831 
 832       // Store bound argument into new stack slot
 833       oop arg = java_lang_invoke_BoundMethodHandle::argument(method_handle);
 834       if (arg_type == T_OBJECT) {
 835         assert(arg_slots == 1, "should be");
 836         SET_VMSLOTS_OBJECT(arg, arg_slot);
 837       }
 838       else {
 839         jvalue arg_value;
 840         arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
 841         switch (arg_type) {
 842         case T_BOOLEAN:
 843           SET_VMSLOTS_INT(arg_value.z, arg_slot);
 844           break;
 845         case T_CHAR:
 846           SET_VMSLOTS_INT(arg_value.c, arg_slot);
 847           break;
 848         case T_BYTE:
 849           SET_VMSLOTS_INT(arg_value.b, arg_slot);
 850           break;
 851         case T_SHORT:
 852           SET_VMSLOTS_INT(arg_value.s, arg_slot);
 853           break;
 854         case T_INT:
 855           SET_VMSLOTS_INT(arg_value.i, arg_slot);
 856           break;
 857         case T_FLOAT:
 858           SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
 859           break;
 860         case T_LONG:
 861           SET_VMSLOTS_LONG(arg_value.j, arg_slot + 1);
 862           break;
 863         case T_DOUBLE:
 864           SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot + 1);
 865           break;
 866         default:
 867           tty->print_cr("unhandled type %s", type2name(arg_type));
 868           ShouldNotReachHere();
 869         }
 870       }
 871     }
 872     break;
 873 
 874   case MethodHandles::_adapter_retype_only:
 875   case MethodHandles::_adapter_retype_raw:
 876     src_rtype = result_type_of_handle(
 877       java_lang_invoke_MethodHandle::vmtarget(method_handle));
 878     dst_rtype = result_type_of_handle(method_handle);
 879     break;
 880 
 881   case MethodHandles::_adapter_check_cast:
 882     {
 883       int arg_slot =
 884         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
 885       oop arg = VMSLOTS_OBJECT(arg_slot);
 886       if (arg != NULL) {
 887         klassOop objKlassOop = arg->klass();
 888         klassOop klassOf = java_lang_Class::as_klassOop(
 889           java_lang_invoke_AdapterMethodHandle::argument(method_handle));
 890 
 891         if (objKlassOop != klassOf &&
 892             !objKlassOop->klass_part()->is_subtype_of(klassOf)) {
 893           ResourceMark rm(THREAD);
 894           const char* objName = Klass::cast(objKlassOop)->external_name();
 895           const char* klassName = Klass::cast(klassOf)->external_name();
 896           char* message = SharedRuntime::generate_class_cast_message(
 897             objName, klassName);
 898 
 899           stack->set_sp(calculate_unwind_sp(stack, method_handle));
 900           CALL_VM_NOCHECK_NOFIX(
 901             throw_exception(
 902               thread, vmSymbols::java_lang_ClassCastException(), message));
 903           // NB all oops trashed!
 904           assert(HAS_PENDING_EXCEPTION, "should do");
 905           return;
 906         }
 907       }
 908     }
 909     break;
 910 
 911   case MethodHandles::_adapter_dup_args:
 912     {
 913       int arg_slot =
 914         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
 915       int conv =
 916         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
 917       int num_slots = -MethodHandles::adapter_conversion_stack_move(conv);
 918       assert(num_slots > 0, "should be");
 919 
 920       // Create the new slot(s)
 921       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
 922       stack->overflow_check(num_slots, THREAD);
 923       if (HAS_PENDING_EXCEPTION) {
 924         // all oops trashed
 925         stack->set_sp(unwind_sp);
 926         return;
 927       }
 928 
 929       // Duplicate the arguments
 930       for (int i = num_slots - 1; i >= 0; i--)
 931         stack->push(*VMSLOTS_SLOT(arg_slot + i));
 932 
 933       vmslots = stack->sp(); // unused, but let the compiler figure that out
 934     }
 935     break;
 936 
 937   case MethodHandles::_adapter_drop_args:
 938     {
 939       int arg_slot =
 940         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
 941       int conv =
 942         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
 943       int num_slots = MethodHandles::adapter_conversion_stack_move(conv);
 944       assert(num_slots > 0, "should be");
 945 
 946       remove_vmslots(arg_slot, num_slots, THREAD); // doesn't trap
 947       vmslots = stack->sp(); // unused, but let the compiler figure that out
 948     }
 949     break;
 950 
 951   case MethodHandles::_adapter_opt_swap_1:
 952   case MethodHandles::_adapter_opt_swap_2:
 953   case MethodHandles::_adapter_opt_rot_1_up:
 954   case MethodHandles::_adapter_opt_rot_1_down:
 955   case MethodHandles::_adapter_opt_rot_2_up:
 956   case MethodHandles::_adapter_opt_rot_2_down:
 957     {
 958       int arg1 =
 959         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
 960       int conv =
 961         java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
 962       int arg2 = MethodHandles::adapter_conversion_vminfo(conv);
 963 
 964       int swap_bytes = 0, rotate = 0;
 965       MethodHandles::get_ek_adapter_opt_swap_rot_info(
 966         entry_kind, swap_bytes, rotate);
 967       int swap_slots = swap_bytes >> LogBytesPerWord;
 968 
 969       intptr_t tmp;
 970       switch (rotate) {
 971       case 0: // swap
 972         for (int i = 0; i < swap_slots; i++) {
 973           tmp = *VMSLOTS_SLOT(arg1 + i);
 974           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(arg2 + i), arg1 + i);
 975           SET_VMSLOTS_SLOT(&tmp, arg2 + i);
 976         }
 977         break;
 978 
 979       case 1: // up
 980         assert(arg1 - swap_slots > arg2, "should be");
 981 
 982         tmp = *VMSLOTS_SLOT(arg1);
 983         for (int i = arg1 - swap_slots; i >= arg2; i--)
 984           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + swap_slots);
 985         SET_VMSLOTS_SLOT(&tmp, arg2);
 986 
 987         break;
 988 
 989       case -1: // down
 990         assert(arg2 - swap_slots > arg1, "should be");
 991 
 992         tmp = *VMSLOTS_SLOT(arg1);
 993         for (int i = arg1 + swap_slots; i <= arg2; i++)
 994           SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i - swap_slots);
 995         SET_VMSLOTS_SLOT(&tmp, arg2);
 996         break;
 997 
 998       default:
 999         ShouldNotReachHere();
1000       }
1001     }
1002     break;
1003 
1004   case MethodHandles::_adapter_opt_i2l:
1005     {
1006       int arg_slot =
1007         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
1008       int arg = VMSLOTS_INT(arg_slot);
1009       intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
1010       insert_vmslots(arg_slot, 1, THREAD);
1011       if (HAS_PENDING_EXCEPTION) {
1012         // all oops trashed
1013         stack->set_sp(unwind_sp);
1014         return;
1015       }
1016       vmslots = stack->sp();
1017       arg_slot++;
1018       SET_VMSLOTS_LONG(arg, arg_slot);
1019     }
1020     break;
1021 
1022   case MethodHandles::_adapter_opt_unboxi:
1023   case MethodHandles::_adapter_opt_unboxl:
1024     {
1025       int arg_slot =
1026         java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
1027       oop arg = VMSLOTS_OBJECT(arg_slot);
1028       jvalue arg_value;
1029       BasicType arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
1030       if (arg_type == T_LONG || arg_type == T_DOUBLE) {
1031         intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
1032         insert_vmslots(arg_slot, 1, THREAD);
1033         if (HAS_PENDING_EXCEPTION) {
1034           // all oops trashed
1035           stack->set_sp(unwind_sp);
1036           return;
1037         }
1038         vmslots = stack->sp();
1039         arg_slot++;
1040       }
1041       switch (arg_type) {
1042       case T_BOOLEAN:
1043         SET_VMSLOTS_INT(arg_value.z, arg_slot);
1044         break;
1045       case T_CHAR:
1046         SET_VMSLOTS_INT(arg_value.c, arg_slot);
1047         break;
1048       case T_BYTE:
1049         SET_VMSLOTS_INT(arg_value.b, arg_slot);
1050         break;
1051       case T_SHORT:
1052         SET_VMSLOTS_INT(arg_value.s, arg_slot);
1053         break;
1054       case T_INT:
1055         SET_VMSLOTS_INT(arg_value.i, arg_slot);
1056         break;
1057       case T_FLOAT:
1058         SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
1059         break;
1060       case T_LONG:
1061         SET_VMSLOTS_LONG(arg_value.j, arg_slot);
1062         break;
1063       case T_DOUBLE:
1064         SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot);
1065         break;
1066       default:
1067         tty->print_cr("unhandled type %s", type2name(arg_type));
1068         ShouldNotReachHere();
1069       }
1070     }
1071     break;
1072 
1073   default:
1074     tty->print_cr("unhandled entry_kind %s",
1075                   MethodHandles::entry_name(entry_kind));
1076     ShouldNotReachHere();
1077   }
1078 
1079   // Continue along the chain
1080   if (direct_to_method) {
1081     if (method == NULL) {
1082       method =
1083         (methodOop) java_lang_invoke_MethodHandle::vmtarget(method_handle);
1084     }
1085     address entry_point = method->from_interpreted_entry();
1086     Interpreter::invoke_method(method, entry_point, THREAD);
1087   }
1088   else {
1089     process_method_handle(
1090       java_lang_invoke_MethodHandle::vmtarget(method_handle), THREAD);
1091   }
1092   // NB all oops now trashed
1093 
1094   // Adapt the result type, if necessary
1095   if (src_rtype != dst_rtype && !HAS_PENDING_EXCEPTION) {
1096     switch (dst_rtype) {
1097     case T_VOID:
1098       for (int i = 0; i < type2size[src_rtype]; i++)
1099         stack->pop();
1100       return;
1101 
1102     case T_INT:
1103       switch (src_rtype) {
1104       case T_VOID:
1105         stack->overflow_check(1, CHECK);
1106         stack->push(0);
1107         return;
1108 
1109       case T_BOOLEAN:
1110       case T_CHAR:
1111       case T_BYTE:
1112       case T_SHORT:
1113         return;
1114       }
1115     }
1116 
1117     tty->print_cr("unhandled conversion:");
1118     tty->print_cr("src_rtype = %s", type2name(src_rtype));
1119     tty->print_cr("dst_rtype = %s", type2name(dst_rtype));
1120     ShouldNotReachHere();
1121   }
1122 }
1123 
1124 // The new slots will be inserted before slot insert_before.
1125 // Slots < insert_before will have the same slot number after the insert.
1126 // Slots >= insert_before will become old_slot + num_slots.
1127 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
1128   JavaThread *thread = (JavaThread *) THREAD;
1129   ZeroStack *stack = thread->zero_stack();
1130 
1131   // Allocate the space
1132   stack->overflow_check(num_slots, CHECK);
1133   stack->alloc(num_slots * wordSize);
1134   intptr_t *vmslots = stack->sp();
1135 
1136   // Shuffle everything up
1137   for (int i = 0; i < insert_before; i++)
1138     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
1139 }
1140 
1141 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
1142   JavaThread *thread = (JavaThread *) THREAD;
1143   ZeroStack *stack = thread->zero_stack();
1144   intptr_t *vmslots = stack->sp();
1145 
1146   // Move everything down
1147   for (int i = first_slot - 1; i >= 0; i--)
1148     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
1149 
1150   // Deallocate the space
1151   stack->set_sp(stack->sp() + num_slots);
1152 }
1153 
1154 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
1155   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
1156   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
1157   return java_lang_Class::as_BasicType(return_type, (klassOop *) NULL);
1158 }
1159 
1160 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
1161                                               oop method_handle) {
1162   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
1163   oop form = java_lang_invoke_MethodType::form(method_type);
1164   int argument_slots = java_lang_invoke_MethodTypeForm::vmslots(form);
1165 
1166   return stack->sp() + argument_slots;
1167 }
1168 
1169 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
1170                                                 Symbol*     name,
1171                                                 char*       message))
1172   THROW_MSG(name, message);
1173 IRT_END
1174 
1175 InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) {
1176   JavaThread *thread = (JavaThread *) THREAD;
1177   ZeroStack *stack = thread->zero_stack();
1178 
1179   // Calculate the size of the frame we'll build, including
1180   // any adjustments to the caller's frame that we'll make.
1181   int extra_locals  = 0;
1182   int monitor_words = 0;
1183   int stack_words   = 0;
1184 
1185   if (!method->is_native()) {
1186     extra_locals = method->max_locals() - method->size_of_parameters();
1187     stack_words  = method->max_stack();
1188   }
1189   if (method->is_synchronized()) {
1190     monitor_words = frame::interpreter_frame_monitor_size();
1191   }
1192   stack->overflow_check(
1193     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
1194 
1195   // Adjust the caller's stack frame to accomodate any additional
1196   // local variables we have contiguously with our parameters.
1197   for (int i = 0; i < extra_locals; i++)
1198     stack->push(0);
1199 
1200   intptr_t *locals;
1201   if (method->is_native())
1202     locals = stack->sp() + (method->size_of_parameters() - 1);
1203   else
1204     locals = stack->sp() + (method->max_locals() - 1);
1205 
1206   stack->push(0); // next_frame, filled in later
1207   intptr_t *fp = stack->sp();
1208   assert(fp - stack->sp() == next_frame_off, "should be");
1209 
1210   stack->push(INTERPRETER_FRAME);
1211   assert(fp - stack->sp() == frame_type_off, "should be");
1212 
1213   interpreterState istate =
1214     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
1215   assert(fp - stack->sp() == istate_off, "should be");
1216 
1217   istate->set_locals(locals);
1218   istate->set_method(method);
1219   istate->set_self_link(istate);
1220   istate->set_prev_link(NULL);
1221   istate->set_thread(thread);
1222   istate->set_bcp(method->is_native() ? NULL : method->code_base());
1223   istate->set_constants(method->constants()->cache());
1224   istate->set_msg(BytecodeInterpreter::method_entry);
1225   istate->set_oop_temp(NULL);
1226   istate->set_mdx(NULL);
1227   istate->set_callee(NULL);
1228 
1229   istate->set_monitor_base((BasicObjectLock *) stack->sp());
1230   if (method->is_synchronized()) {
1231     BasicObjectLock *monitor =
1232       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
1233     oop object;
1234     if (method->is_static())
1235       object = method->constants()->pool_holder()->java_mirror();
1236     else
1237       object = (oop) locals[0];
1238     monitor->set_obj(object);
1239   }
1240 
1241   istate->set_stack_base(stack->sp());
1242   istate->set_stack(stack->sp() - 1);
1243   if (stack_words)
1244     stack->alloc(stack_words * wordSize);
1245   istate->set_stack_limit(stack->sp() - 1);
1246 
1247   return (InterpreterFrame *) fp;
1248 }
1249 
1250 int AbstractInterpreter::BasicType_as_index(BasicType type) {
1251   int i = 0;
1252   switch (type) {
1253     case T_BOOLEAN: i = 0; break;
1254     case T_CHAR   : i = 1; break;
1255     case T_BYTE   : i = 2; break;
1256     case T_SHORT  : i = 3; break;
1257     case T_INT    : i = 4; break;
1258     case T_LONG   : i = 5; break;
1259     case T_VOID   : i = 6; break;
1260     case T_FLOAT  : i = 7; break;
1261     case T_DOUBLE : i = 8; break;
1262     case T_OBJECT : i = 9; break;
1263     case T_ARRAY  : i = 9; break;
1264     default       : ShouldNotReachHere();
1265   }
1266   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
1267          "index out of bounds");
1268   return i;
1269 }
1270 
1271 BasicType CppInterpreter::result_type_of(methodOop method) {
1272   BasicType t;
1273   switch (method->result_index()) {
1274     case 0 : t = T_BOOLEAN; break;
1275     case 1 : t = T_CHAR;    break;
1276     case 2 : t = T_BYTE;    break;
1277     case 3 : t = T_SHORT;   break;
1278     case 4 : t = T_INT;     break;
1279     case 5 : t = T_LONG;    break;
1280     case 6 : t = T_VOID;    break;
1281     case 7 : t = T_FLOAT;   break;
1282     case 8 : t = T_DOUBLE;  break;
1283     case 9 : t = T_OBJECT;  break;
1284     default: ShouldNotReachHere();
1285   }
1286   assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
1287          "out of step with AbstractInterpreter::BasicType_as_index");
1288   return t;
1289 }
1290 
1291 address InterpreterGenerator::generate_empty_entry() {
1292   if (!UseFastEmptyMethods)
1293     return NULL;
1294 
1295   return generate_entry((address) CppInterpreter::empty_entry);
1296 }
1297 
1298 address InterpreterGenerator::generate_accessor_entry() {
1299   if (!UseFastAccessorMethods)
1300     return NULL;
1301 
1302   return generate_entry((address) CppInterpreter::accessor_entry);
1303 }
1304 
1305 address InterpreterGenerator::generate_Reference_get_entry(void) {
1306 #ifndef SERIALGC
1307   if (UseG1GC) {
1308     // We need to generate have a routine that generates code to:
1309     //   * load the value in the referent field
1310     //   * passes that value to the pre-barrier.
1311     //
1312     // In the case of G1 this will record the value of the
1313     // referent in an SATB buffer if marking is active.
1314     // This will cause concurrent marking to mark the referent
1315     // field as live.
1316     Unimplemented();
1317   }
1318 #endif // SERIALGC
1319 
1320   // If G1 is not enabled then attempt to go through the accessor entry point
1321   // Reference.get is an accessor
1322   return generate_accessor_entry();
1323 }
1324 
1325 address InterpreterGenerator::generate_native_entry(bool synchronized) {
1326   assert(synchronized == false, "should be");
1327 
1328   return generate_entry((address) CppInterpreter::native_entry);
1329 }
1330 
1331 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1332   assert(synchronized == false, "should be");
1333 
1334   return generate_entry((address) CppInterpreter::normal_entry);
1335 }
1336 
1337 address AbstractInterpreterGenerator::generate_method_entry(
1338     AbstractInterpreter::MethodKind kind) {
1339   address entry_point = NULL;
1340 
1341   switch (kind) {
1342   case Interpreter::zerolocals:
1343   case Interpreter::zerolocals_synchronized:
1344     break;
1345 
1346   case Interpreter::native:
1347     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
1348     break;
1349 
1350   case Interpreter::native_synchronized:
1351     entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
1352     break;
1353 
1354   case Interpreter::empty:
1355     entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();
1356     break;
1357 
1358   case Interpreter::accessor:
1359     entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();
1360     break;
1361 
1362   case Interpreter::abstract:
1363     entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();
1364     break;
1365 
1366   case Interpreter::method_handle:
1367     entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
1368     break;
1369 
1370   case Interpreter::java_lang_math_sin:
1371   case Interpreter::java_lang_math_cos:
1372   case Interpreter::java_lang_math_tan:
1373   case Interpreter::java_lang_math_abs:
1374   case Interpreter::java_lang_math_log:
1375   case Interpreter::java_lang_math_log10:
1376   case Interpreter::java_lang_math_sqrt:
1377     entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
1378     break;
1379 
1380   case Interpreter::java_lang_ref_reference_get:
1381     entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
1382     break;
1383 
1384   default:
1385     ShouldNotReachHere();
1386   }
1387 
1388   if (entry_point == NULL)
1389     entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);
1390 
1391   return entry_point;
1392 }
1393 
1394 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1395  : CppInterpreterGenerator(code) {
1396    generate_all();
1397 }
1398 
1399 // Deoptimization helpers
1400 
1401 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
1402   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
1403 
1404   int size_in_words = size >> LogBytesPerWord;
1405   assert(size_in_words * wordSize == size, "unaligned");
1406   assert(size_in_words >= header_words, "too small");
1407   stack->overflow_check(size_in_words, CHECK_NULL);
1408 
1409   stack->push(0); // next_frame, filled in later
1410   intptr_t *fp = stack->sp();
1411   assert(fp - stack->sp() == next_frame_off, "should be");
1412 
1413   stack->push(INTERPRETER_FRAME);
1414   assert(fp - stack->sp() == frame_type_off, "should be");
1415 
1416   interpreterState istate =
1417     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
1418   assert(fp - stack->sp() == istate_off, "should be");
1419   istate->set_self_link(NULL); // mark invalid
1420 
1421   stack->alloc((size_in_words - header_words) * wordSize);
1422 
1423   return (InterpreterFrame *) fp;
1424 }
1425 
1426 int AbstractInterpreter::layout_activation(methodOop method,
1427                                            int       tempcount,
1428                                            int       popframe_extra_args,
1429                                            int       moncount,
1430                                            int       caller_actual_parameters,
1431                                            int       callee_param_count,
1432                                            int       callee_locals,
1433                                            frame*    caller,
1434                                            frame*    interpreter_frame,
1435                                            bool      is_top_frame) {
1436   assert(popframe_extra_args == 0, "what to do?");
1437   assert(!is_top_frame || (!callee_locals && !callee_param_count),
1438          "top frame should have no caller");
1439 
1440   // This code must exactly match what InterpreterFrame::build
1441   // does (the full InterpreterFrame::build, that is, not the
1442   // one that creates empty frames for the deoptimizer).
1443   //
1444   // If interpreter_frame is not NULL then it will be filled in.
1445   // It's size is determined by a previous call to this method,
1446   // so it should be correct.
1447   //
1448   // Note that tempcount is the current size of the expression
1449   // stack.  For top most frames we will allocate a full sized
1450   // expression stack and not the trimmed version that non-top
1451   // frames have.
1452 
1453   int header_words        = InterpreterFrame::header_words;
1454   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
1455   int stack_words         = is_top_frame ? method->max_stack() : tempcount;
1456   int callee_extra_locals = callee_locals - callee_param_count;
1457 
1458   if (interpreter_frame) {
1459     intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
1460     interpreterState istate = interpreter_frame->get_interpreterState();
1461     intptr_t *monitor_base  = (intptr_t*) istate;
1462     intptr_t *stack_base    = monitor_base - monitor_words;
1463     intptr_t *stack         = stack_base - tempcount - 1;
1464 
1465     BytecodeInterpreter::layout_interpreterState(istate,
1466                                                  caller,
1467                                                  NULL,
1468                                                  method,
1469                                                  locals,
1470                                                  stack,
1471                                                  stack_base,
1472                                                  monitor_base,
1473                                                  NULL,
1474                                                  is_top_frame);
1475   }
1476   return header_words + monitor_words + stack_words + callee_extra_locals;
1477 }
1478 
1479 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
1480                                                   frame*    caller,
1481                                                   frame*    current,
1482                                                   methodOop method,
1483                                                   intptr_t* locals,
1484                                                   intptr_t* stack,
1485                                                   intptr_t* stack_base,
1486                                                   intptr_t* monitor_base,
1487                                                   intptr_t* frame_bottom,
1488                                                   bool      is_top_frame) {
1489   istate->set_locals(locals);
1490   istate->set_method(method);
1491   istate->set_self_link(istate);
1492   istate->set_prev_link(NULL);
1493   // thread will be set by a hacky repurposing of frame::patch_pc()
1494   // bcp will be set by vframeArrayElement::unpack_on_stack()
1495   istate->set_constants(method->constants()->cache());
1496   istate->set_msg(BytecodeInterpreter::method_resume);
1497   istate->set_bcp_advance(0);
1498   istate->set_oop_temp(NULL);
1499   istate->set_mdx(NULL);
1500   if (caller->is_interpreted_frame()) {
1501     interpreterState prev = caller->get_interpreterState();
1502     prev->set_callee(method);
1503     if (*prev->bcp() == Bytecodes::_invokeinterface)
1504       prev->set_bcp_advance(5);
1505     else
1506       prev->set_bcp_advance(3);
1507   }
1508   istate->set_callee(NULL);
1509   istate->set_monitor_base((BasicObjectLock *) monitor_base);
1510   istate->set_stack_base(stack_base);
1511   istate->set_stack(stack);
1512   istate->set_stack_limit(stack_base - method->max_stack() - 1);
1513 }
1514 
1515 address CppInterpreter::return_entry(TosState state, int length) {
1516   ShouldNotCallThis();
1517 }
1518 
1519 address CppInterpreter::deopt_entry(TosState state, int length) {
1520   return NULL;
1521 }
1522 
1523 // Helper for (runtime) stack overflow checks
1524 
1525 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
1526   return 0;
1527 }
1528 
1529 // Helper for figuring out if frames are interpreter frames
1530 
1531 bool CppInterpreter::contains(address pc) {
1532 #ifdef PRODUCT
1533   ShouldNotCallThis();
1534 #else
1535   return false; // make frame::print_value_on work
1536 #endif // !PRODUCT
1537 }
1538 
1539 // Result handlers and convertors
1540 
1541 address CppInterpreterGenerator::generate_result_handler_for(
1542     BasicType type) {
1543   assembler()->advance(1);
1544   return ShouldNotCallThisStub();
1545 }
1546 
1547 address CppInterpreterGenerator::generate_tosca_to_stack_converter(
1548     BasicType type) {
1549   assembler()->advance(1);
1550   return ShouldNotCallThisStub();
1551 }
1552 
1553 address CppInterpreterGenerator::generate_stack_to_stack_converter(
1554     BasicType type) {
1555   assembler()->advance(1);
1556   return ShouldNotCallThisStub();
1557 }
1558 
1559 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
1560     BasicType type) {
1561   assembler()->advance(1);
1562   return ShouldNotCallThisStub();
1563 }
1564 
1565 #endif // CC_INTERP