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