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