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