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