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