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