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