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