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/cppInterpreterGenerator.hpp"
  31 #include "interpreter/interpreter.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/freturn/lreturn/dreturn
 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::_freturn ||
 507           code[4] == Bytecodes::_lreturn ||
 508           code[4] == Bytecodes::_dreturn ||
 509           code[4] == Bytecodes::_areturn), "should do");
 510   u2 index = Bytes::get_native_u2(&code[2]);
 511 
 512   // Get the entry from the constant pool cache, and drop into
 513   // the slow path if it has not been resolved
 514   ConstantPoolCache* cache = method->constants()->cache();
 515   ConstantPoolCacheEntry* entry = cache->entry_at(index);
 516   if (!entry->is_resolved(Bytecodes::_getfield)) {
 517     return normal_entry(method, 0, THREAD);
 518   }
 519 
 520   // Get the result and push it onto the stack
 521   switch (entry->flag_state()) {
 522   case ltos:
 523   case dtos:
 524     stack->overflow_check(1, CHECK_0);
 525     stack->alloc(wordSize);
 526     break;
 527   }
 528   if (entry->is_volatile()) {
 529     switch (entry->flag_state()) {
 530     case ctos:
 531       SET_LOCALS_INT(object->char_field_acquire(entry->f2_as_index()), 0);
 532       break;
 533 
 534     case btos:
 535       SET_LOCALS_INT(object->byte_field_acquire(entry->f2_as_index()), 0);
 536       break;
 537 
 538     case stos:
 539       SET_LOCALS_INT(object->short_field_acquire(entry->f2_as_index()), 0);
 540       break;
 541 
 542     case itos:
 543       SET_LOCALS_INT(object->int_field_acquire(entry->f2_as_index()), 0);
 544       break;
 545 
 546     case ltos:
 547       SET_LOCALS_LONG(object->long_field_acquire(entry->f2_as_index()), 0);
 548       break;
 549 
 550     case ftos:
 551       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2_as_index()), 0);
 552       break;
 553 
 554     case dtos:
 555       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2_as_index()), 0);
 556       break;
 557 
 558     case atos:
 559       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2_as_index()), 0);
 560       break;
 561 
 562     default:
 563       ShouldNotReachHere();
 564     }
 565   }
 566   else {
 567     switch (entry->flag_state()) {
 568     case ctos:
 569       SET_LOCALS_INT(object->char_field(entry->f2_as_index()), 0);
 570       break;
 571 
 572     case btos:
 573       SET_LOCALS_INT(object->byte_field(entry->f2_as_index()), 0);
 574       break;
 575 
 576     case stos:
 577       SET_LOCALS_INT(object->short_field(entry->f2_as_index()), 0);
 578       break;
 579 
 580     case itos:
 581       SET_LOCALS_INT(object->int_field(entry->f2_as_index()), 0);
 582       break;
 583 
 584     case ltos:
 585       SET_LOCALS_LONG(object->long_field(entry->f2_as_index()), 0);
 586       break;
 587 
 588     case ftos:
 589       SET_LOCALS_FLOAT(object->float_field(entry->f2_as_index()), 0);
 590       break;
 591 
 592     case dtos:
 593       SET_LOCALS_DOUBLE(object->double_field(entry->f2_as_index()), 0);
 594       break;
 595 
 596     case atos:
 597       SET_LOCALS_OBJECT(object->obj_field(entry->f2_as_index()), 0);
 598       break;
 599 
 600     default:
 601       ShouldNotReachHere();
 602     }
 603   }
 604 
 605   // No deoptimized frames on the stack
 606   return 0;
 607 }
 608 
 609 int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
 610   JavaThread *thread = (JavaThread *) THREAD;
 611   ZeroStack *stack = thread->zero_stack();
 612 
 613   // Drop into the slow path if we need a safepoint check
 614   if (SafepointSynchronize::do_call_back()) {
 615     return normal_entry(method, 0, THREAD);
 616   }
 617 
 618   // Pop our parameters
 619   stack->set_sp(stack->sp() + method->size_of_parameters());
 620 
 621   // No deoptimized frames on the stack
 622   return 0;
 623 }
 624 
 625 // The new slots will be inserted before slot insert_before.
 626 // Slots < insert_before will have the same slot number after the insert.
 627 // Slots >= insert_before will become old_slot + num_slots.
 628 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
 629   JavaThread *thread = (JavaThread *) THREAD;
 630   ZeroStack *stack = thread->zero_stack();
 631 
 632   // Allocate the space
 633   stack->overflow_check(num_slots, CHECK);
 634   stack->alloc(num_slots * wordSize);
 635   intptr_t *vmslots = stack->sp();
 636 
 637   // Shuffle everything up
 638   for (int i = 0; i < insert_before; i++)
 639     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
 640 }
 641 
 642 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
 643   JavaThread *thread = (JavaThread *) THREAD;
 644   ZeroStack *stack = thread->zero_stack();
 645   intptr_t *vmslots = stack->sp();
 646 
 647   // Move everything down
 648   for (int i = first_slot - 1; i >= 0; i--)
 649     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
 650 
 651   // Deallocate the space
 652   stack->set_sp(stack->sp() + num_slots);
 653 }
 654 
 655 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
 656   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
 657   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
 658   return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
 659 }
 660 
 661 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
 662                                               oop method_handle) {
 663   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
 664   int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
 665 
 666   return stack->sp() + argument_slots;
 667 }
 668 
 669 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
 670                                                 Symbol*     name,
 671                                                 char*       message))
 672   THROW_MSG(name, message);
 673 IRT_END
 674 
 675 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
 676   JavaThread *thread = (JavaThread *) THREAD;
 677   ZeroStack *stack = thread->zero_stack();
 678 
 679   // Calculate the size of the frame we'll build, including
 680   // any adjustments to the caller's frame that we'll make.
 681   int extra_locals  = 0;
 682   int monitor_words = 0;
 683   int stack_words   = 0;
 684 
 685   if (!method->is_native()) {
 686     extra_locals = method->max_locals() - method->size_of_parameters();
 687     stack_words  = method->max_stack();
 688   }
 689   if (method->is_synchronized()) {
 690     monitor_words = frame::interpreter_frame_monitor_size();
 691   }
 692   stack->overflow_check(
 693     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
 694 
 695   // Adjust the caller's stack frame to accomodate any additional
 696   // local variables we have contiguously with our parameters.
 697   for (int i = 0; i < extra_locals; i++)
 698     stack->push(0);
 699 
 700   intptr_t *locals;
 701   if (method->is_native())
 702     locals = stack->sp() + (method->size_of_parameters() - 1);
 703   else
 704     locals = stack->sp() + (method->max_locals() - 1);
 705 
 706   stack->push(0); // next_frame, filled in later
 707   intptr_t *fp = stack->sp();
 708   assert(fp - stack->sp() == next_frame_off, "should be");
 709 
 710   stack->push(INTERPRETER_FRAME);
 711   assert(fp - stack->sp() == frame_type_off, "should be");
 712 
 713   interpreterState istate =
 714     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 715   assert(fp - stack->sp() == istate_off, "should be");
 716 
 717   istate->set_locals(locals);
 718   istate->set_method(method);
 719   istate->set_self_link(istate);
 720   istate->set_prev_link(NULL);
 721   istate->set_thread(thread);
 722   istate->set_bcp(method->is_native() ? NULL : method->code_base());
 723   istate->set_constants(method->constants()->cache());
 724   istate->set_msg(BytecodeInterpreter::method_entry);
 725   istate->set_oop_temp(NULL);
 726   istate->set_mdx(NULL);
 727   istate->set_callee(NULL);
 728 
 729   istate->set_monitor_base((BasicObjectLock *) stack->sp());
 730   if (method->is_synchronized()) {
 731     BasicObjectLock *monitor =
 732       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
 733     oop object;
 734     if (method->is_static())
 735       object = method->constants()->pool_holder()->java_mirror();
 736     else
 737       object = (oop) (void*)locals[0];
 738     monitor->set_obj(object);
 739   }
 740 
 741   istate->set_stack_base(stack->sp());
 742   istate->set_stack(stack->sp() - 1);
 743   if (stack_words)
 744     stack->alloc(stack_words * wordSize);
 745   istate->set_stack_limit(stack->sp() - 1);
 746 
 747   return (InterpreterFrame *) fp;
 748 }
 749 
 750 int AbstractInterpreter::BasicType_as_index(BasicType type) {
 751   int i = 0;
 752   switch (type) {
 753     case T_BOOLEAN: i = 0; break;
 754     case T_CHAR   : i = 1; break;
 755     case T_BYTE   : i = 2; break;
 756     case T_SHORT  : i = 3; break;
 757     case T_INT    : i = 4; break;
 758     case T_LONG   : i = 5; break;
 759     case T_VOID   : i = 6; break;
 760     case T_FLOAT  : i = 7; break;
 761     case T_DOUBLE : i = 8; break;
 762     case T_OBJECT : i = 9; break;
 763     case T_ARRAY  : i = 9; break;
 764     default       : ShouldNotReachHere();
 765   }
 766   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
 767          "index out of bounds");
 768   return i;
 769 }
 770 
 771 BasicType CppInterpreter::result_type_of(Method* method) {
 772   BasicType t;
 773   switch (method->result_index()) {
 774     case 0 : t = T_BOOLEAN; break;
 775     case 1 : t = T_CHAR;    break;
 776     case 2 : t = T_BYTE;    break;
 777     case 3 : t = T_SHORT;   break;
 778     case 4 : t = T_INT;     break;
 779     case 5 : t = T_LONG;    break;
 780     case 6 : t = T_VOID;    break;
 781     case 7 : t = T_FLOAT;   break;
 782     case 8 : t = T_DOUBLE;  break;
 783     case 9 : t = T_OBJECT;  break;
 784     default: ShouldNotReachHere();
 785   }
 786   assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
 787          "out of step with AbstractInterpreter::BasicType_as_index");
 788   return t;
 789 }
 790 
 791 address CppInterpreterGenerator::generate_empty_entry() {
 792   if (!UseFastEmptyMethods)
 793     return NULL;
 794 
 795   return generate_entry((address) CppInterpreter::empty_entry);
 796 }
 797 
 798 address CppInterpreterGenerator::generate_accessor_entry() {
 799   if (!UseFastAccessorMethods)
 800     return NULL;
 801 
 802   return generate_entry((address) CppInterpreter::accessor_entry);
 803 }
 804 
 805 address CppInterpreterGenerator::generate_Reference_get_entry(void) {
 806 #if INCLUDE_ALL_GCS
 807   if (UseG1GC) {
 808     // We need to generate have a routine that generates code to:
 809     //   * load the value in the referent field
 810     //   * passes that value to the pre-barrier.
 811     //
 812     // In the case of G1 this will record the value of the
 813     // referent in an SATB buffer if marking is active.
 814     // This will cause concurrent marking to mark the referent
 815     // field as live.
 816     Unimplemented();
 817   }
 818 #endif // INCLUDE_ALL_GCS
 819 
 820   // If G1 is not enabled then attempt to go through the normal entry point
 821   // Reference.get could be instrumented by jvmti
 822   return NULL;
 823 }
 824 
 825 address CppInterpreterGenerator::generate_native_entry(bool synchronized) {
 826   return generate_entry((address) CppInterpreter::native_entry);
 827 }
 828 
 829 address CppInterpreterGenerator::generate_normal_entry(bool synchronized) {
 830   return generate_entry((address) CppInterpreter::normal_entry);
 831 }
 832 
 833 
 834 // Deoptimization helpers
 835 
 836 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
 837   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 838 
 839   int size_in_words = size >> LogBytesPerWord;
 840   assert(size_in_words * wordSize == size, "unaligned");
 841   assert(size_in_words >= header_words, "too small");
 842   stack->overflow_check(size_in_words, CHECK_NULL);
 843 
 844   stack->push(0); // next_frame, filled in later
 845   intptr_t *fp = stack->sp();
 846   assert(fp - stack->sp() == next_frame_off, "should be");
 847 
 848   stack->push(INTERPRETER_FRAME);
 849   assert(fp - stack->sp() == frame_type_off, "should be");
 850 
 851   interpreterState istate =
 852     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 853   assert(fp - stack->sp() == istate_off, "should be");
 854   istate->set_self_link(NULL); // mark invalid
 855 
 856   stack->alloc((size_in_words - header_words) * wordSize);
 857 
 858   return (InterpreterFrame *) fp;
 859 }
 860 
 861 int AbstractInterpreter::size_activation(int       max_stack,
 862                                          int       tempcount,
 863                                          int       extra_args,
 864                                          int       moncount,
 865                                          int       callee_param_count,
 866                                          int       callee_locals,
 867                                          bool      is_top_frame) {
 868   int header_words        = InterpreterFrame::header_words;
 869   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
 870   int stack_words         = is_top_frame ? max_stack : tempcount;
 871   int callee_extra_locals = callee_locals - callee_param_count;
 872 
 873   return header_words + monitor_words + stack_words + callee_extra_locals;
 874 }
 875 
 876 void AbstractInterpreter::layout_activation(Method* method,
 877                                             int       tempcount,
 878                                             int       popframe_extra_args,
 879                                             int       moncount,
 880                                             int       caller_actual_parameters,
 881                                             int       callee_param_count,
 882                                             int       callee_locals,
 883                                             frame*    caller,
 884                                             frame*    interpreter_frame,
 885                                             bool      is_top_frame,
 886                                             bool      is_bottom_frame) {
 887   assert(popframe_extra_args == 0, "what to do?");
 888   assert(!is_top_frame || (!callee_locals && !callee_param_count),
 889          "top frame should have no caller");
 890 
 891   // This code must exactly match what InterpreterFrame::build
 892   // does (the full InterpreterFrame::build, that is, not the
 893   // one that creates empty frames for the deoptimizer).
 894   //
 895   // interpreter_frame will be filled in.  It's size is determined by
 896   // a previous call to the size_activation() method,
 897   //
 898   // Note that tempcount is the current size of the expression
 899   // stack.  For top most frames we will allocate a full sized
 900   // expression stack and not the trimmed version that non-top
 901   // frames have.
 902 
 903   int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
 904   intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
 905   interpreterState istate = interpreter_frame->get_interpreterState();
 906   intptr_t *monitor_base  = (intptr_t*) istate;
 907   intptr_t *stack_base    = monitor_base - monitor_words;
 908   intptr_t *stack         = stack_base - tempcount - 1;
 909 
 910   BytecodeInterpreter::layout_interpreterState(istate,
 911                                                caller,
 912                                                NULL,
 913                                                method,
 914                                                locals,
 915                                                stack,
 916                                                stack_base,
 917                                                monitor_base,
 918                                                NULL,
 919                                                is_top_frame);
 920 }
 921 
 922 void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
 923                                                   frame*    caller,
 924                                                   frame*    current,
 925                                                   Method* method,
 926                                                   intptr_t* locals,
 927                                                   intptr_t* stack,
 928                                                   intptr_t* stack_base,
 929                                                   intptr_t* monitor_base,
 930                                                   intptr_t* frame_bottom,
 931                                                   bool      is_top_frame) {
 932   istate->set_locals(locals);
 933   istate->set_method(method);
 934   istate->set_self_link(istate);
 935   istate->set_prev_link(NULL);
 936   // thread will be set by a hacky repurposing of frame::patch_pc()
 937   // bcp will be set by vframeArrayElement::unpack_on_stack()
 938   istate->set_constants(method->constants()->cache());
 939   istate->set_msg(BytecodeInterpreter::method_resume);
 940   istate->set_bcp_advance(0);
 941   istate->set_oop_temp(NULL);
 942   istate->set_mdx(NULL);
 943   if (caller->is_interpreted_frame()) {
 944     interpreterState prev = caller->get_interpreterState();
 945     prev->set_callee(method);
 946     if (*prev->bcp() == Bytecodes::_invokeinterface)
 947       prev->set_bcp_advance(5);
 948     else
 949       prev->set_bcp_advance(3);
 950   }
 951   istate->set_callee(NULL);
 952   istate->set_monitor_base((BasicObjectLock *) monitor_base);
 953   istate->set_stack_base(stack_base);
 954   istate->set_stack(stack);
 955   istate->set_stack_limit(stack_base - method->max_stack() - 1);
 956 }
 957 
 958 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 959   ShouldNotCallThis();
 960   return NULL;
 961 }
 962 
 963 address CppInterpreter::deopt_entry(TosState state, int length) {
 964   return NULL;
 965 }
 966 
 967 // Helper for (runtime) stack overflow checks
 968 
 969 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
 970   return 0;
 971 }
 972 
 973 // Helper for figuring out if frames are interpreter frames
 974 
 975 bool CppInterpreter::contains(address pc) {
 976   return false; // make frame::print_value_on work
 977 }
 978 #endif // CC_INTERP