1 /*
   2  * Copyright (c) 2003, 2017, 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.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 
  54 #ifdef CC_INTERP
  55 
  56 #define fixup_after_potential_safepoint()       \
  57   method = istate->method()
  58 
  59 #define CALL_VM_NOCHECK_NOFIX(func)             \
  60   thread->set_last_Java_frame();                \
  61   func;                                         \
  62   thread->reset_last_Java_frame();
  63 
  64 #define CALL_VM_NOCHECK(func)                   \
  65   CALL_VM_NOCHECK_NOFIX(func)                   \
  66   fixup_after_potential_safepoint()
  67 
  68 int CppInterpreter::normal_entry(Method* method, intptr_t UNUSED, TRAPS) {
  69   JavaThread *thread = (JavaThread *) THREAD;
  70 
  71   // Allocate and initialize our frame.
  72   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
  73   thread->push_zero_frame(frame);
  74 
  75   // Execute those bytecodes!
  76   main_loop(0, THREAD);
  77 
  78   // No deoptimized frames on the stack
  79   return 0;
  80 }
  81 
  82 intptr_t narrow(BasicType type, intptr_t result) {
  83   // mask integer result to narrower return type.
  84   switch (type) {
  85     case T_BOOLEAN:
  86       return result&1;
  87     case T_BYTE:
  88       return (intptr_t)(jbyte)result;
  89     case T_CHAR:
  90       return (intptr_t)(uintptr_t)(jchar)result;
  91     case T_SHORT:
  92       return (intptr_t)(jshort)result;
  93     case T_OBJECT:  // nothing to do fall through
  94     case T_ARRAY:
  95     case T_LONG:
  96     case T_INT:
  97     case T_FLOAT:
  98     case T_DOUBLE:
  99     case T_VOID:
 100       return result;
 101     default  : ShouldNotReachHere();
 102   }
 103 }
 104 
 105 
 106 void CppInterpreter::main_loop(int recurse, TRAPS) {
 107   JavaThread *thread = (JavaThread *) THREAD;
 108   ZeroStack *stack = thread->zero_stack();
 109 
 110   // If we are entering from a deopt we may need to call
 111   // ourself a few times in order to get to our frame.
 112   if (recurse)
 113     main_loop(recurse - 1, THREAD);
 114 
 115   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 116   interpreterState istate = frame->interpreter_state();
 117   Method* method = istate->method();
 118 
 119   intptr_t *result = NULL;
 120   int result_slots = 0;
 121 
 122   while (true) {
 123     // We can set up the frame anchor with everything we want at
 124     // this point as we are thread_in_Java and no safepoints can
 125     // occur until we go to vm mode.  We do have to clear flags
 126     // on return from vm but that is it.
 127     thread->set_last_Java_frame();
 128 
 129     // Call the interpreter
 130     if (JvmtiExport::can_post_interpreter_events())
 131       BytecodeInterpreter::runWithChecks(istate);
 132     else
 133       BytecodeInterpreter::run(istate);
 134     fixup_after_potential_safepoint();
 135 
 136     // Clear the frame anchor
 137     thread->reset_last_Java_frame();
 138 
 139     // Examine the message from the interpreter to decide what to do
 140     if (istate->msg() == BytecodeInterpreter::call_method) {
 141       Method* callee = istate->callee();
 142 
 143       // Trim back the stack to put the parameters at the top
 144       stack->set_sp(istate->stack() + 1);
 145 
 146       // Make the call
 147       Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
 148       fixup_after_potential_safepoint();
 149 
 150       // Convert the result
 151       istate->set_stack(stack->sp() - 1);
 152 
 153       // Restore the stack
 154       stack->set_sp(istate->stack_limit() + 1);
 155 
 156       // Resume the interpreter
 157       istate->set_msg(BytecodeInterpreter::method_resume);
 158     }
 159     else if (istate->msg() == BytecodeInterpreter::more_monitors) {
 160       int monitor_words = frame::interpreter_frame_monitor_size();
 161 
 162       // Allocate the space
 163       stack->overflow_check(monitor_words, THREAD);
 164       if (HAS_PENDING_EXCEPTION)
 165         break;
 166       stack->alloc(monitor_words * wordSize);
 167 
 168       // Move the expression stack contents
 169       for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
 170         *(p - monitor_words) = *p;
 171 
 172       // Move the expression stack pointers
 173       istate->set_stack_limit(istate->stack_limit() - monitor_words);
 174       istate->set_stack(istate->stack() - monitor_words);
 175       istate->set_stack_base(istate->stack_base() - monitor_words);
 176 
 177       // Zero the new monitor so the interpreter can find it.
 178       ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);
 179 
 180       // Resume the interpreter
 181       istate->set_msg(BytecodeInterpreter::got_monitors);
 182     }
 183     else if (istate->msg() == BytecodeInterpreter::return_from_method) {
 184       // Copy the result into the caller's frame
 185       result_slots = type2size[method->result_type()];
 186       assert(result_slots >= 0 && result_slots <= 2, "what?");
 187       result = istate->stack() + result_slots;
 188       break;
 189     }
 190     else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
 191       assert(HAS_PENDING_EXCEPTION, "should do");
 192       break;
 193     }
 194     else if (istate->msg() == BytecodeInterpreter::do_osr) {
 195       // Unwind the current frame
 196       thread->pop_zero_frame();
 197 
 198       // Remove any extension of the previous frame
 199       int extra_locals = method->max_locals() - method->size_of_parameters();
 200       stack->set_sp(stack->sp() + extra_locals);
 201 
 202       // Jump into the OSR method
 203       Interpreter::invoke_osr(
 204         method, istate->osr_entry(), istate->osr_buf(), THREAD);
 205       return;
 206     }
 207     else {
 208       ShouldNotReachHere();
 209     }
 210   }
 211 
 212   // Unwind the current frame
 213   thread->pop_zero_frame();
 214 
 215   // Pop our local variables
 216   stack->set_sp(stack->sp() + method->max_locals());
 217 
 218   // Push our result
 219   for (int i = 0; i < result_slots; i++) {
 220     // Adjust result to smaller
 221     union {
 222       intptr_t res;
 223       jint res_jint;
 224     };
 225     res = result[-i];
 226     if (result_slots == 1) {
 227       BasicType t = method->result_type();
 228       if (is_subword_type(t)) {
 229         res_jint = (jint)narrow(t, res_jint);
 230       }
 231     }
 232     stack->push(res);
 233   }
 234 }
 235 
 236 int CppInterpreter::native_entry(Method* method, intptr_t UNUSED, TRAPS) {
 237   // Make sure method is native and not abstract
 238   assert(method->is_native() && !method->is_abstract(), "should be");
 239 
 240   JavaThread *thread = (JavaThread *) THREAD;
 241   ZeroStack *stack = thread->zero_stack();
 242 
 243   // Allocate and initialize our frame
 244   InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
 245   thread->push_zero_frame(frame);
 246   interpreterState istate = frame->interpreter_state();
 247   intptr_t *locals = istate->locals();
 248 
 249   // Update the invocation counter
 250   if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
 251     MethodCounters* mcs = method->method_counters();
 252     if (mcs == NULL) {
 253       CALL_VM_NOCHECK(mcs = InterpreterRuntime::build_method_counters(thread, method));
 254       if (HAS_PENDING_EXCEPTION)
 255         goto unwind_and_return;
 256     }
 257     InvocationCounter *counter = mcs->invocation_counter();
 258     counter->increment();
 259     if (counter->reached_InvocationLimit(mcs->backedge_counter())) {
 260       CALL_VM_NOCHECK(
 261         InterpreterRuntime::frequency_counter_overflow(thread, NULL));
 262       if (HAS_PENDING_EXCEPTION)
 263         goto unwind_and_return;
 264     }
 265   }
 266 
 267   // Lock if necessary
 268   BasicObjectLock *monitor;
 269   monitor = NULL;
 270   if (method->is_synchronized()) {
 271     monitor = (BasicObjectLock*) istate->stack_base();
 272     oop lockee = monitor->obj();
 273     markOop disp = lockee->mark()->set_unlocked();
 274 
 275     monitor->lock()->set_displaced_header(disp);
 276     if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
 277       if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
 278         monitor->lock()->set_displaced_header(NULL);
 279       }
 280       else {
 281         CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
 282         if (HAS_PENDING_EXCEPTION)
 283           goto unwind_and_return;
 284       }
 285     }
 286   }
 287 
 288   // Get the signature handler
 289   InterpreterRuntime::SignatureHandler *handler; {
 290     address handlerAddr = method->signature_handler();
 291     if (handlerAddr == NULL) {
 292       CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
 293       if (HAS_PENDING_EXCEPTION)
 294         goto unlock_unwind_and_return;
 295 
 296       handlerAddr = method->signature_handler();
 297       assert(handlerAddr != NULL, "eh?");
 298     }
 299     if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
 300       CALL_VM_NOCHECK(handlerAddr =
 301         InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
 302       if (HAS_PENDING_EXCEPTION)
 303         goto unlock_unwind_and_return;
 304     }
 305     handler = \
 306       InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
 307   }
 308 
 309   // Get the native function entry point
 310   address function;
 311   function = method->native_function();
 312   assert(function != NULL, "should be set if signature handler is");
 313 
 314   // Build the argument list
 315   stack->overflow_check(handler->argument_count() * 2, THREAD);
 316   if (HAS_PENDING_EXCEPTION)
 317     goto unlock_unwind_and_return;
 318 
 319   void **arguments;
 320   void *mirror; {
 321     arguments =
 322       (void **) stack->alloc(handler->argument_count() * sizeof(void **));
 323     void **dst = arguments;
 324 
 325     void *env = thread->jni_environment();
 326     *(dst++) = &env;
 327 
 328     if (method->is_static()) {
 329       istate->set_oop_temp(
 330         method->constants()->pool_holder()->java_mirror());
 331       mirror = istate->oop_temp_addr();
 332       *(dst++) = &mirror;
 333     }
 334 
 335     intptr_t *src = locals;
 336     for (int i = dst - arguments; i < handler->argument_count(); i++) {
 337       ffi_type *type = handler->argument_type(i);
 338       if (type == &ffi_type_pointer) {
 339         if (*src) {
 340           stack->push((intptr_t) src);
 341           *(dst++) = stack->sp();
 342         }
 343         else {
 344           *(dst++) = src;
 345         }
 346         src--;
 347       }
 348       else if (type->size == 4) {
 349         *(dst++) = src--;
 350       }
 351       else if (type->size == 8) {
 352         src--;
 353         *(dst++) = src--;
 354       }
 355       else {
 356         ShouldNotReachHere();
 357       }
 358     }
 359   }
 360 
 361   // Set up the Java frame anchor
 362   thread->set_last_Java_frame();
 363 
 364   // Change the thread state to _thread_in_native
 365   ThreadStateTransition::transition_from_java(thread, _thread_in_native);
 366 
 367   // Make the call
 368   intptr_t result[4 - LogBytesPerWord];
 369   ffi_call(handler->cif(), (void (*)()) function, result, arguments);
 370 
 371   // Change the thread state back to _thread_in_Java.
 372   // ThreadStateTransition::transition_from_native() cannot be used
 373   // here because it does not check for asynchronous exceptions.
 374   // We have to manage the transition ourself.
 375   thread->set_thread_state(_thread_in_native_trans);
 376 
 377   // Make sure new state is visible in the GC thread
 378   InterfaceSupport::serialize_thread_state(thread);
 379 
 380   // Handle safepoint operations, pending suspend requests,
 381   // and pending asynchronous exceptions.
 382   if (SafepointSynchronize::do_call_back() ||
 383       thread->has_special_condition_for_native_trans()) {
 384     JavaThread::check_special_condition_for_native_trans(thread);
 385     CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
 386   }
 387 
 388   // Finally we can change the thread state to _thread_in_Java.
 389   thread->set_thread_state(_thread_in_Java);
 390   fixup_after_potential_safepoint();
 391 
 392   // Clear the frame anchor
 393   thread->reset_last_Java_frame();
 394 
 395   // If the result was an oop then unbox it and store it in
 396   // oop_temp where the garbage collector can see it before
 397   // we release the handle it might be protected by.
 398   if (handler->result_type() == &ffi_type_pointer) {
 399     if (result[0] == 0) {
 400       istate->set_oop_temp(NULL);
 401     } else {
 402       jobject handle = reinterpret_cast<jobject>(result[0]);
 403       istate->set_oop_temp(JNIHandles::resolve(handle));
 404     }
 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/freturn/lreturn/dreturn
 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::_freturn ||
 536           code[4] == Bytecodes::_lreturn ||
 537           code[4] == Bytecodes::_dreturn ||
 538           code[4] == Bytecodes::_areturn), "should do");
 539   u2 index = Bytes::get_native_u2(&code[2]);
 540 
 541   // Get the entry from the constant pool cache, and drop into
 542   // the slow path if it has not been resolved
 543   ConstantPoolCache* cache = method->constants()->cache();
 544   ConstantPoolCacheEntry* entry = cache->entry_at(index);
 545   if (!entry->is_resolved(Bytecodes::_getfield)) {
 546     return normal_entry(method, 0, THREAD);
 547   }
 548 
 549   // Get the result and push it onto the stack
 550   switch (entry->flag_state()) {
 551   case ltos:
 552   case dtos:
 553     stack->overflow_check(1, CHECK_0);
 554     stack->alloc(wordSize);
 555     break;
 556   }
 557   if (entry->is_volatile()) {
 558     switch (entry->flag_state()) {
 559     case ctos:
 560       SET_LOCALS_INT(object->char_field_acquire(entry->f2_as_index()), 0);
 561       break;
 562 
 563     case btos:
 564     case ztos:
 565       SET_LOCALS_INT(object->byte_field_acquire(entry->f2_as_index()), 0);
 566       break;
 567 
 568     case stos:
 569       SET_LOCALS_INT(object->short_field_acquire(entry->f2_as_index()), 0);
 570       break;
 571 
 572     case itos:
 573       SET_LOCALS_INT(object->int_field_acquire(entry->f2_as_index()), 0);
 574       break;
 575 
 576     case ltos:
 577       SET_LOCALS_LONG(object->long_field_acquire(entry->f2_as_index()), 0);
 578       break;
 579 
 580     case ftos:
 581       SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2_as_index()), 0);
 582       break;
 583 
 584     case dtos:
 585       SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2_as_index()), 0);
 586       break;
 587 
 588     case atos:
 589       SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2_as_index()), 0);
 590       break;
 591 
 592     default:
 593       ShouldNotReachHere();
 594     }
 595   }
 596   else {
 597     switch (entry->flag_state()) {
 598     case ctos:
 599       SET_LOCALS_INT(object->char_field(entry->f2_as_index()), 0);
 600       break;
 601 
 602     case btos:
 603     case ztos:
 604       SET_LOCALS_INT(object->byte_field(entry->f2_as_index()), 0);
 605       break;
 606 
 607     case stos:
 608       SET_LOCALS_INT(object->short_field(entry->f2_as_index()), 0);
 609       break;
 610 
 611     case itos:
 612       SET_LOCALS_INT(object->int_field(entry->f2_as_index()), 0);
 613       break;
 614 
 615     case ltos:
 616       SET_LOCALS_LONG(object->long_field(entry->f2_as_index()), 0);
 617       break;
 618 
 619     case ftos:
 620       SET_LOCALS_FLOAT(object->float_field(entry->f2_as_index()), 0);
 621       break;
 622 
 623     case dtos:
 624       SET_LOCALS_DOUBLE(object->double_field(entry->f2_as_index()), 0);
 625       break;
 626 
 627     case atos:
 628       SET_LOCALS_OBJECT(object->obj_field(entry->f2_as_index()), 0);
 629       break;
 630 
 631     default:
 632       ShouldNotReachHere();
 633     }
 634   }
 635 
 636   // No deoptimized frames on the stack
 637   return 0;
 638 }
 639 
 640 int CppInterpreter::empty_entry(Method* method, intptr_t UNUSED, TRAPS) {
 641   JavaThread *thread = (JavaThread *) THREAD;
 642   ZeroStack *stack = thread->zero_stack();
 643 
 644   // Drop into the slow path if we need a safepoint check
 645   if (SafepointSynchronize::do_call_back()) {
 646     return normal_entry(method, 0, THREAD);
 647   }
 648 
 649   // Pop our parameters
 650   stack->set_sp(stack->sp() + method->size_of_parameters());
 651 
 652   // No deoptimized frames on the stack
 653   return 0;
 654 }
 655 
 656 // The new slots will be inserted before slot insert_before.
 657 // Slots < insert_before will have the same slot number after the insert.
 658 // Slots >= insert_before will become old_slot + num_slots.
 659 void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
 660   JavaThread *thread = (JavaThread *) THREAD;
 661   ZeroStack *stack = thread->zero_stack();
 662 
 663   // Allocate the space
 664   stack->overflow_check(num_slots, CHECK);
 665   stack->alloc(num_slots * wordSize);
 666   intptr_t *vmslots = stack->sp();
 667 
 668   // Shuffle everything up
 669   for (int i = 0; i < insert_before; i++)
 670     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
 671 }
 672 
 673 void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
 674   JavaThread *thread = (JavaThread *) THREAD;
 675   ZeroStack *stack = thread->zero_stack();
 676   intptr_t *vmslots = stack->sp();
 677 
 678   // Move everything down
 679   for (int i = first_slot - 1; i >= 0; i--)
 680     SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);
 681 
 682   // Deallocate the space
 683   stack->set_sp(stack->sp() + num_slots);
 684 }
 685 
 686 BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
 687   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
 688   oop return_type = java_lang_invoke_MethodType::rtype(method_type);
 689   return java_lang_Class::as_BasicType(return_type, (Klass* *) NULL);
 690 }
 691 
 692 intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
 693                                               oop method_handle) {
 694   oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
 695   int argument_slots = java_lang_invoke_MethodType::ptype_slot_count(method_type);
 696 
 697   return stack->sp() + argument_slots;
 698 }
 699 
 700 IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
 701                                                 Symbol*     name,
 702                                                 char*       message))
 703   THROW_MSG(name, message);
 704 IRT_END
 705 
 706 InterpreterFrame *InterpreterFrame::build(Method* const method, TRAPS) {
 707   JavaThread *thread = (JavaThread *) THREAD;
 708   ZeroStack *stack = thread->zero_stack();
 709 
 710   // Calculate the size of the frame we'll build, including
 711   // any adjustments to the caller's frame that we'll make.
 712   int extra_locals  = 0;
 713   int monitor_words = 0;
 714   int stack_words   = 0;
 715 
 716   if (!method->is_native()) {
 717     extra_locals = method->max_locals() - method->size_of_parameters();
 718     stack_words  = method->max_stack();
 719   }
 720   if (method->is_synchronized()) {
 721     monitor_words = frame::interpreter_frame_monitor_size();
 722   }
 723   stack->overflow_check(
 724     extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);
 725 
 726   // Adjust the caller's stack frame to accomodate any additional
 727   // local variables we have contiguously with our parameters.
 728   for (int i = 0; i < extra_locals; i++)
 729     stack->push(0);
 730 
 731   intptr_t *locals;
 732   if (method->is_native())
 733     locals = stack->sp() + (method->size_of_parameters() - 1);
 734   else
 735     locals = stack->sp() + (method->max_locals() - 1);
 736 
 737   stack->push(0); // next_frame, filled in later
 738   intptr_t *fp = stack->sp();
 739   assert(fp - stack->sp() == next_frame_off, "should be");
 740 
 741   stack->push(INTERPRETER_FRAME);
 742   assert(fp - stack->sp() == frame_type_off, "should be");
 743 
 744   interpreterState istate =
 745     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 746   assert(fp - stack->sp() == istate_off, "should be");
 747 
 748   istate->set_locals(locals);
 749   istate->set_method(method);
 750   istate->set_mirror(method->method_holder()->java_mirror());
 751   istate->set_self_link(istate);
 752   istate->set_prev_link(NULL);
 753   istate->set_thread(thread);
 754   istate->set_bcp(method->is_native() ? NULL : method->code_base());
 755   istate->set_constants(method->constants()->cache());
 756   istate->set_msg(BytecodeInterpreter::method_entry);
 757   istate->set_oop_temp(NULL);
 758   istate->set_mdx(NULL);
 759   istate->set_callee(NULL);
 760 
 761   istate->set_monitor_base((BasicObjectLock *) stack->sp());
 762   if (method->is_synchronized()) {
 763     BasicObjectLock *monitor =
 764       (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
 765     oop object;
 766     if (method->is_static())
 767       object = method->constants()->pool_holder()->java_mirror();
 768     else
 769       object = (oop) (void*)locals[0];
 770     monitor->set_obj(object);
 771   }
 772 
 773   istate->set_stack_base(stack->sp());
 774   istate->set_stack(stack->sp() - 1);
 775   if (stack_words)
 776     stack->alloc(stack_words * wordSize);
 777   istate->set_stack_limit(stack->sp() - 1);
 778 
 779   return (InterpreterFrame *) fp;
 780 }
 781 
 782 InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
 783   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 784 
 785   int size_in_words = size >> LogBytesPerWord;
 786   assert(size_in_words * wordSize == size, "unaligned");
 787   assert(size_in_words >= header_words, "too small");
 788   stack->overflow_check(size_in_words, CHECK_NULL);
 789 
 790   stack->push(0); // next_frame, filled in later
 791   intptr_t *fp = stack->sp();
 792   assert(fp - stack->sp() == next_frame_off, "should be");
 793 
 794   stack->push(INTERPRETER_FRAME);
 795   assert(fp - stack->sp() == frame_type_off, "should be");
 796 
 797   interpreterState istate =
 798     (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
 799   assert(fp - stack->sp() == istate_off, "should be");
 800   istate->set_self_link(NULL); // mark invalid
 801 
 802   stack->alloc((size_in_words - header_words) * wordSize);
 803 
 804   return (InterpreterFrame *) fp;
 805 }
 806 
 807 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 808   ShouldNotCallThis();
 809   return NULL;
 810 }
 811 
 812 address CppInterpreter::deopt_entry(TosState state, int length) {
 813   return NULL;
 814 }
 815 
 816 // Helper for figuring out if frames are interpreter frames
 817 
 818 bool CppInterpreter::contains(address pc) {
 819   return false; // make frame::print_value_on work
 820 }
 821 #endif // CC_INTERP