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