1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "gc_interface/collectedHeap.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "interpreter/templateTable.hpp"
  35 #include "memory/oopFactory.hpp"
  36 #include "memory/universe.inline.hpp"
  37 #include "oops/constantPool.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/methodData.hpp"
  40 #include "oops/objArrayKlass.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "oops/symbol.hpp"
  43 #include "prims/jvmtiExport.hpp"
  44 #include "prims/nativeLookup.hpp"
  45 #include "runtime/biasedLocking.hpp"
  46 #include "runtime/compilationPolicy.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/fieldDescriptor.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/jfieldIDWorkaround.hpp"
  53 #include "runtime/osThread.hpp"
  54 #include "runtime/sharedRuntime.hpp"
  55 #include "runtime/stubRoutines.hpp"
  56 #include "runtime/synchronizer.hpp"
  57 #include "runtime/threadCritical.hpp"
  58 #include "utilities/events.hpp"
  59 #ifdef TARGET_ARCH_x86
  60 # include "vm_version_x86.hpp"
  61 #endif
  62 #ifdef TARGET_ARCH_sparc
  63 # include "vm_version_sparc.hpp"
  64 #endif
  65 #ifdef TARGET_ARCH_zero
  66 # include "vm_version_zero.hpp"
  67 #endif
  68 #ifdef TARGET_ARCH_arm
  69 # include "vm_version_arm.hpp"
  70 #endif
  71 #ifdef TARGET_ARCH_ppc
  72 # include "vm_version_ppc.hpp"
  73 #endif
  74 #ifdef COMPILER2
  75 #include "opto/runtime.hpp"
  76 #endif
  77 
  78 class UnlockFlagSaver {
  79   private:
  80     JavaThread* _thread;
  81     bool _do_not_unlock;
  82   public:
  83     UnlockFlagSaver(JavaThread* t) {
  84       _thread = t;
  85       _do_not_unlock = t->do_not_unlock_if_synchronized();
  86       t->set_do_not_unlock_if_synchronized(false);
  87     }
  88     ~UnlockFlagSaver() {
  89       _thread->set_do_not_unlock_if_synchronized(_do_not_unlock);
  90     }
  91 };
  92 
  93 //------------------------------------------------------------------------------------------------------------------------
  94 // State accessors
  95 
  96 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread *thread) {
  97   last_frame(thread).interpreter_frame_set_bcp(bcp);
  98   if (ProfileInterpreter) {
  99     // ProfileTraps uses MDOs independently of ProfileInterpreter.
 100     // That is why we must check both ProfileInterpreter and mdo != NULL.
 101     MethodData* mdo = last_frame(thread).interpreter_frame_method()->method_data();
 102     if (mdo != NULL) {
 103       NEEDS_CLEANUP;
 104       last_frame(thread).interpreter_frame_set_mdp(mdo->bci_to_dp(last_frame(thread).interpreter_frame_bci()));
 105     }
 106   }
 107 }
 108 
 109 //------------------------------------------------------------------------------------------------------------------------
 110 // Constants
 111 
 112 
 113 IRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* thread, bool wide))
 114   // access constant pool
 115   ConstantPool* pool = method(thread)->constants();
 116   int index = wide ? get_index_u2(thread, Bytecodes::_ldc_w) : get_index_u1(thread, Bytecodes::_ldc);
 117   constantTag tag = pool->tag_at(index);
 118 
 119   assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
 120   Klass* klass = pool->klass_at(index, CHECK);
 121     oop java_class = klass->java_mirror();
 122     thread->set_vm_result(java_class);
 123 IRT_END
 124 
 125 IRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* thread, Bytecodes::Code bytecode)) {
 126   assert(bytecode == Bytecodes::_fast_aldc ||
 127          bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
 128   ResourceMark rm(thread);
 129   methodHandle m (thread, method(thread));
 130   Bytecode_loadconstant ldc(m, bci(thread));
 131   oop result = ldc.resolve_constant(CHECK);
 132 #ifdef ASSERT
 133   {
 134     // The bytecode wrappers aren't GC-safe so construct a new one
 135     Bytecode_loadconstant ldc2(m, bci(thread));
 136     oop coop = m->constants()->resolved_references()->obj_at(ldc2.cache_index());
 137     assert(result == coop, "expected result for assembly code");
 138   }
 139 #endif
 140   thread->set_vm_result(result);
 141 }
 142 IRT_END
 143 
 144 
 145 //------------------------------------------------------------------------------------------------------------------------
 146 // Allocation
 147 
 148 IRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* thread, ConstantPool* pool, int index))
 149   Klass* k_oop = pool->klass_at(index, CHECK);
 150   instanceKlassHandle klass (THREAD, k_oop);
 151 
 152   // Make sure we are not instantiating an abstract klass
 153   klass->check_valid_for_instantiation(true, CHECK);
 154 
 155   // Make sure klass is initialized
 156   klass->initialize(CHECK);
 157 
 158   // At this point the class may not be fully initialized
 159   // because of recursive initialization. If it is fully
 160   // initialized & has_finalized is not set, we rewrite
 161   // it into its fast version (Note: no locking is needed
 162   // here since this is an atomic byte write and can be
 163   // done more than once).
 164   //
 165   // Note: In case of classes with has_finalized we don't
 166   //       rewrite since that saves us an extra check in
 167   //       the fast version which then would call the
 168   //       slow version anyway (and do a call back into
 169   //       Java).
 170   //       If we have a breakpoint, then we don't rewrite
 171   //       because the _breakpoint bytecode would be lost.
 172   oop obj = klass->allocate_instance(CHECK);
 173   thread->set_vm_result(obj);
 174 IRT_END
 175 
 176 
 177 IRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* thread, BasicType type, jint size))
 178   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 179   thread->set_vm_result(obj);
 180 IRT_END
 181 
 182 
 183 IRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* thread, ConstantPool* pool, int index, jint size))
 184   // Note: no oopHandle for pool & klass needed since they are not used
 185   //       anymore after new_objArray() and no GC can happen before.
 186   //       (This may have to change if this code changes!)
 187   Klass*    klass = pool->klass_at(index, CHECK);
 188   objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 189   thread->set_vm_result(obj);
 190 IRT_END
 191 
 192 
 193 IRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* thread, jint* first_size_address))
 194   // We may want to pass in more arguments - could make this slightly faster
 195   ConstantPool* constants = method(thread)->constants();
 196   int          i = get_index_u2(thread, Bytecodes::_multianewarray);
 197   Klass* klass = constants->klass_at(i, CHECK);
 198   int   nof_dims = number_of_dimensions(thread);
 199   assert(klass->is_klass(), "not a class");
 200   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 201 
 202   // We must create an array of jints to pass to multi_allocate.
 203   ResourceMark rm(thread);
 204   const int small_dims = 10;
 205   jint dim_array[small_dims];
 206   jint *dims = &dim_array[0];
 207   if (nof_dims > small_dims) {
 208     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 209   }
 210   for (int index = 0; index < nof_dims; index++) {
 211     // offset from first_size_address is addressed as local[index]
 212     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 213     dims[index] = first_size_address[n];
 214   }
 215   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 216   thread->set_vm_result(obj);
 217 IRT_END
 218 
 219 
 220 IRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* thread, oopDesc* obj))
 221   assert(obj->is_oop(), "must be a valid oop");
 222   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 223   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 224 IRT_END
 225 
 226 
 227 // Quicken instance-of and check-cast bytecodes
 228 IRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* thread))
 229   // Force resolving; quicken the bytecode
 230   int which = get_index_u2(thread, Bytecodes::_checkcast);
 231   ConstantPool* cpool = method(thread)->constants();
 232   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 233   // program we might have seen an unquick'd bytecode in the interpreter but have another
 234   // thread quicken the bytecode before we get here.
 235   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 236   Klass* klass = cpool->klass_at(which, CHECK);
 237   thread->set_vm_result_2(klass);
 238 IRT_END
 239 
 240 
 241 //------------------------------------------------------------------------------------------------------------------------
 242 // Exceptions
 243 
 244 void InterpreterRuntime::note_trap_inner(JavaThread* thread, int reason,
 245                                          methodHandle trap_method, int trap_bci, TRAPS) {
 246   if (trap_method.not_null()) {
 247     MethodData* trap_mdo = trap_method->method_data();
 248     if (trap_mdo == NULL) {
 249       Method::build_interpreter_method_data(trap_method, THREAD);
 250       if (HAS_PENDING_EXCEPTION) {
 251         assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())),
 252                "we expect only an OOM error here");
 253         CLEAR_PENDING_EXCEPTION;
 254       }
 255       trap_mdo = trap_method->method_data();
 256       // and fall through...
 257     }
 258     if (trap_mdo != NULL) {
 259       // Update per-method count of trap events.  The interpreter
 260       // is updating the MDO to simulate the effect of compiler traps.
 261       Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
 262     }
 263   }
 264 }
 265 
 266 // Assume the compiler is (or will be) interested in this event.
 267 // If necessary, create an MDO to hold the information, and record it.
 268 void InterpreterRuntime::note_trap(JavaThread* thread, int reason, TRAPS) {
 269   assert(ProfileTraps, "call me only if profiling");
 270   methodHandle trap_method(thread, method(thread));
 271   int trap_bci = trap_method->bci_from(bcp(thread));
 272   note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
 273 }
 274 
 275 #ifdef CC_INTERP
 276 // As legacy note_trap, but we have more arguments.
 277 IRT_ENTRY(void, InterpreterRuntime::note_trap(JavaThread* thread, int reason, Method *method, int trap_bci))
 278   methodHandle trap_method(method);
 279   note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
 280 IRT_END
 281 
 282 // Class Deoptimization is not visible in BytecodeInterpreter, so we need a wrapper
 283 // for each exception.
 284 void InterpreterRuntime::note_nullCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 285   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_null_check, method, trap_bci); }
 286 void InterpreterRuntime::note_div0Check_trap(JavaThread* thread, Method *method, int trap_bci)
 287   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_div0_check, method, trap_bci); }
 288 void InterpreterRuntime::note_rangeCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 289   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_range_check, method, trap_bci); }
 290 void InterpreterRuntime::note_classCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 291   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_class_check, method, trap_bci); }
 292 void InterpreterRuntime::note_arrayCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 293   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_array_check, method, trap_bci); }
 294 #endif // CC_INTERP
 295 
 296 
 297 static Handle get_preinitialized_exception(Klass* k, TRAPS) {
 298   // get klass
 299   InstanceKlass* klass = InstanceKlass::cast(k);
 300   assert(klass->is_initialized(),
 301          "this klass should have been initialized during VM initialization");
 302   // create instance - do not call constructor since we may have no
 303   // (java) stack space left (should assert constructor is empty)
 304   Handle exception;
 305   oop exception_oop = klass->allocate_instance(CHECK_(exception));
 306   exception = Handle(THREAD, exception_oop);
 307   if (StackTraceInThrowable) {
 308     java_lang_Throwable::fill_in_stack_trace(exception);
 309   }
 310   return exception;
 311 }
 312 
 313 // Special handling for stack overflow: since we don't have any (java) stack
 314 // space left we use the pre-allocated & pre-initialized StackOverflowError
 315 // klass to create an stack overflow error instance.  We do not call its
 316 // constructor for the same reason (it is empty, anyway).
 317 IRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* thread))
 318   Handle exception = get_preinitialized_exception(
 319                                  SystemDictionary::StackOverflowError_klass(),
 320                                  CHECK);
 321   THROW_HANDLE(exception);
 322 IRT_END
 323 
 324 
 325 IRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* thread, char* name, char* message))
 326   // lookup exception klass
 327   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 328   if (ProfileTraps) {
 329     if (s == vmSymbols::java_lang_ArithmeticException()) {
 330       note_trap(thread, Deoptimization::Reason_div0_check, CHECK);
 331     } else if (s == vmSymbols::java_lang_NullPointerException()) {
 332       note_trap(thread, Deoptimization::Reason_null_check, CHECK);
 333     }
 334   }
 335   // create exception
 336   Handle exception = Exceptions::new_exception(thread, s, message);
 337   thread->set_vm_result(exception());
 338 IRT_END
 339 
 340 
 341 IRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* thread, char* name, oopDesc* obj))
 342   ResourceMark rm(thread);
 343   const char* klass_name = obj->klass()->external_name();
 344   // lookup exception klass
 345   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 346   if (ProfileTraps) {
 347     note_trap(thread, Deoptimization::Reason_class_check, CHECK);
 348   }
 349   // create exception, with klass name as detail message
 350   Handle exception = Exceptions::new_exception(thread, s, klass_name);
 351   thread->set_vm_result(exception());
 352 IRT_END
 353 
 354 
 355 IRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* thread, char* name, jint index))
 356   char message[jintAsStringSize];
 357   // lookup exception klass
 358   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 359   if (ProfileTraps) {
 360     note_trap(thread, Deoptimization::Reason_range_check, CHECK);
 361   }
 362   // create exception
 363   sprintf(message, "%d", index);
 364   THROW_MSG(s, message);
 365 IRT_END
 366 
 367 IRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
 368   JavaThread* thread, oopDesc* obj))
 369 
 370   ResourceMark rm(thread);
 371   char* message = SharedRuntime::generate_class_cast_message(
 372     thread, obj->klass()->external_name());
 373 
 374   if (ProfileTraps) {
 375     note_trap(thread, Deoptimization::Reason_class_check, CHECK);
 376   }
 377 
 378   // create exception
 379   THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
 380 IRT_END
 381 
 382 // exception_handler_for_exception(...) returns the continuation address,
 383 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
 384 // The exception oop is returned to make sure it is preserved over GC (it
 385 // is only on the stack if the exception was thrown explicitly via athrow).
 386 // During this operation, the expression stack contains the values for the
 387 // bci where the exception happened. If the exception was propagated back
 388 // from a call, the expression stack contains the values for the bci at the
 389 // invoke w/o arguments (i.e., as if one were inside the call).
 390 IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* thread, oopDesc* exception))
 391 
 392   Handle             h_exception(thread, exception);
 393   methodHandle       h_method   (thread, method(thread));
 394   constantPoolHandle h_constants(thread, h_method->constants());
 395   bool               should_repeat;
 396   int                handler_bci;
 397   int                current_bci = bci(thread);
 398 
 399   // Need to do this check first since when _do_not_unlock_if_synchronized
 400   // is set, we don't want to trigger any classloading which may make calls
 401   // into java, or surprisingly find a matching exception handler for bci 0
 402   // since at this moment the method hasn't been "officially" entered yet.
 403   if (thread->do_not_unlock_if_synchronized()) {
 404     ResourceMark rm;
 405     assert(current_bci == 0,  "bci isn't zero for do_not_unlock_if_synchronized");
 406     thread->set_vm_result(exception);
 407 #ifdef CC_INTERP
 408     return (address) -1;
 409 #else
 410     return Interpreter::remove_activation_entry();
 411 #endif
 412   }
 413 
 414   do {
 415     should_repeat = false;
 416 
 417     // assertions
 418 #ifdef ASSERT
 419     assert(h_exception.not_null(), "NULL exceptions should be handled by athrow");
 420     assert(h_exception->is_oop(), "just checking");
 421     // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
 422     if (!(h_exception->is_a(SystemDictionary::Throwable_klass()))) {
 423       if (ExitVMOnVerifyError) vm_exit(-1);
 424       ShouldNotReachHere();
 425     }
 426 #endif
 427 
 428     // tracing
 429     if (TraceExceptions) {
 430       ttyLocker ttyl;
 431       ResourceMark rm(thread);
 432       tty->print_cr("Exception <%s> (" INTPTR_FORMAT ")", h_exception->print_value_string(), (address)h_exception());
 433       tty->print_cr(" thrown in interpreter method <%s>", h_method->print_value_string());
 434       tty->print_cr(" at bci %d for thread " INTPTR_FORMAT, current_bci, thread);
 435     }
 436 // Don't go paging in something which won't be used.
 437 //     else if (extable->length() == 0) {
 438 //       // disabled for now - interpreter is not using shortcut yet
 439 //       // (shortcut is not to call runtime if we have no exception handlers)
 440 //       // warning("performance bug: should not call runtime if method has no exception handlers");
 441 //     }
 442     // for AbortVMOnException flag
 443     NOT_PRODUCT(Exceptions::debug_check_abort(h_exception));
 444 
 445     // exception handler lookup
 446     KlassHandle h_klass(THREAD, h_exception->klass());
 447     handler_bci = Method::fast_exception_handler_bci_for(h_method, h_klass, current_bci, THREAD);
 448     if (HAS_PENDING_EXCEPTION) {
 449       // We threw an exception while trying to find the exception handler.
 450       // Transfer the new exception to the exception handle which will
 451       // be set into thread local storage, and do another lookup for an
 452       // exception handler for this exception, this time starting at the
 453       // BCI of the exception handler which caused the exception to be
 454       // thrown (bug 4307310).
 455       h_exception = Handle(THREAD, PENDING_EXCEPTION);
 456       CLEAR_PENDING_EXCEPTION;
 457       if (handler_bci >= 0) {
 458         current_bci = handler_bci;
 459         should_repeat = true;
 460       }
 461     }
 462   } while (should_repeat == true);
 463 
 464   // notify JVMTI of an exception throw; JVMTI will detect if this is a first
 465   // time throw or a stack unwinding throw and accordingly notify the debugger
 466   if (JvmtiExport::can_post_on_exceptions()) {
 467     JvmtiExport::post_exception_throw(thread, h_method(), bcp(thread), h_exception());
 468   }
 469 
 470 #ifdef CC_INTERP
 471   address continuation = (address)(intptr_t) handler_bci;
 472 #else
 473   address continuation = NULL;
 474 #endif
 475   address handler_pc = NULL;
 476   if (handler_bci < 0 || !thread->reguard_stack((address) &continuation)) {
 477     // Forward exception to callee (leaving bci/bcp untouched) because (a) no
 478     // handler in this method, or (b) after a stack overflow there is not yet
 479     // enough stack space available to reprotect the stack.
 480 #ifndef CC_INTERP
 481     continuation = Interpreter::remove_activation_entry();
 482 #endif
 483     // Count this for compilation purposes
 484     h_method->interpreter_throwout_increment(THREAD);
 485   } else {
 486     // handler in this method => change bci/bcp to handler bci/bcp and continue there
 487     handler_pc = h_method->code_base() + handler_bci;
 488 #ifndef CC_INTERP
 489     set_bcp_and_mdp(handler_pc, thread);
 490     continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
 491 #endif
 492   }
 493   // notify debugger of an exception catch
 494   // (this is good for exceptions caught in native methods as well)
 495   if (JvmtiExport::can_post_on_exceptions()) {
 496     JvmtiExport::notice_unwind_due_to_exception(thread, h_method(), handler_pc, h_exception(), (handler_pc != NULL));
 497   }
 498 
 499   thread->set_vm_result(h_exception());
 500   return continuation;
 501 IRT_END
 502 
 503 
 504 IRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* thread))
 505   assert(thread->has_pending_exception(), "must only ne called if there's an exception pending");
 506   // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
 507 IRT_END
 508 
 509 
 510 IRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* thread))
 511   THROW(vmSymbols::java_lang_AbstractMethodError());
 512 IRT_END
 513 
 514 
 515 IRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* thread))
 516   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 517 IRT_END
 518 
 519 
 520 //------------------------------------------------------------------------------------------------------------------------
 521 // Fields
 522 //
 523 
 524 IRT_ENTRY(void, InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code bytecode))
 525   // resolve field
 526   FieldAccessInfo info;
 527   constantPoolHandle pool(thread, method(thread)->constants());
 528   bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_putstatic);
 529   bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
 530 
 531   {
 532     JvmtiHideSingleStepping jhss(thread);
 533     LinkResolver::resolve_field(info, pool, get_index_u2_cpcache(thread, bytecode),
 534                                 bytecode, false, CHECK);
 535   } // end JvmtiHideSingleStepping
 536 
 537   // check if link resolution caused cpCache to be updated
 538   if (already_resolved(thread)) return;
 539 
 540   // compute auxiliary field attributes
 541   TosState state  = as_TosState(info.field_type());
 542 
 543   // We need to delay resolving put instructions on final fields
 544   // until we actually invoke one. This is required so we throw
 545   // exceptions at the correct place. If we do not resolve completely
 546   // in the current pass, leaving the put_code set to zero will
 547   // cause the next put instruction to reresolve.
 548   Bytecodes::Code put_code = (Bytecodes::Code)0;
 549 
 550   // We also need to delay resolving getstatic instructions until the
 551   // class is intitialized.  This is required so that access to the static
 552   // field will call the initialization function every time until the class
 553   // is completely initialized ala. in 2.17.5 in JVM Specification.
 554   InstanceKlass *klass = InstanceKlass::cast(info.klass()());
 555   bool uninitialized_static = ((bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic) &&
 556                                !klass->is_initialized());
 557   Bytecodes::Code get_code = (Bytecodes::Code)0;
 558 
 559   if (!uninitialized_static) {
 560     get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
 561     if (is_put || !info.access_flags().is_final()) {
 562       put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 563     }
 564   }
 565 
 566   cache_entry(thread)->set_field(
 567     get_code,
 568     put_code,
 569     info.klass(),
 570     info.field_index(),
 571     info.field_offset(),
 572     state,
 573     info.access_flags().is_final(),
 574     info.access_flags().is_volatile(),
 575     pool->pool_holder()
 576   );
 577 IRT_END
 578 
 579 
 580 //------------------------------------------------------------------------------------------------------------------------
 581 // Synchronization
 582 //
 583 // The interpreter's synchronization code is factored out so that it can
 584 // be shared by method invocation and synchronized blocks.
 585 //%note synchronization_3
 586 
 587 //%note monitor_1
 588 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
 589 #ifdef ASSERT
 590   thread->last_frame().interpreter_frame_verify_monitor(elem);
 591 #endif
 592   if (PrintBiasedLockingStatistics) {
 593     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
 594   }
 595   Handle h_obj(thread, elem->obj());
 596   assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
 597          "must be NULL or an object");
 598   if (UseBiasedLocking) {
 599     // Retry fast entry if bias is revoked to avoid unnecessary inflation
 600     ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
 601   } else {
 602     ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
 603   }
 604   assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
 605          "must be NULL or an object");
 606 #ifdef ASSERT
 607   thread->last_frame().interpreter_frame_verify_monitor(elem);
 608 #endif
 609 IRT_END
 610 
 611 
 612 //%note monitor_1
 613 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
 614 #ifdef ASSERT
 615   thread->last_frame().interpreter_frame_verify_monitor(elem);
 616 #endif
 617   Handle h_obj(thread, elem->obj());
 618   assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
 619          "must be NULL or an object");
 620   if (elem == NULL || h_obj()->is_unlocked()) {
 621     THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 622   }
 623   ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);
 624   // Free entry. This must be done here, since a pending exception might be installed on
 625   // exit. If it is not cleared, the exception handling code will try to unlock the monitor again.
 626   elem->set_obj(NULL);
 627 #ifdef ASSERT
 628   thread->last_frame().interpreter_frame_verify_monitor(elem);
 629 #endif
 630 IRT_END
 631 
 632 
 633 IRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* thread))
 634   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 635 IRT_END
 636 
 637 
 638 IRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* thread))
 639   // Returns an illegal exception to install into the current thread. The
 640   // pending_exception flag is cleared so normal exception handling does not
 641   // trigger. Any current installed exception will be overwritten. This
 642   // method will be called during an exception unwind.
 643 
 644   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 645   Handle exception(thread, thread->vm_result());
 646   assert(exception() != NULL, "vm result should be set");
 647   thread->set_vm_result(NULL); // clear vm result before continuing (may cause memory leaks and assert failures)
 648   if (!exception->is_a(SystemDictionary::ThreadDeath_klass())) {
 649     exception = get_preinitialized_exception(
 650                        SystemDictionary::IllegalMonitorStateException_klass(),
 651                        CATCH);
 652   }
 653   thread->set_vm_result(exception());
 654 IRT_END
 655 
 656 
 657 //------------------------------------------------------------------------------------------------------------------------
 658 // Invokes
 659 
 660 IRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* thread, Method* method, address bcp))
 661   return method->orig_bytecode_at(method->bci_from(bcp));
 662 IRT_END
 663 
 664 IRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* thread, Method* method, address bcp, Bytecodes::Code new_code))
 665   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 666 IRT_END
 667 
 668 IRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* thread, Method* method, address bcp))
 669   JvmtiExport::post_raw_breakpoint(thread, method, bcp);
 670 IRT_END
 671 
 672 IRT_ENTRY(void, InterpreterRuntime::resolve_invoke(JavaThread* thread, Bytecodes::Code bytecode)) {
 673   // extract receiver from the outgoing argument list if necessary
 674   Handle receiver(thread, NULL);
 675   if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface) {
 676     ResourceMark rm(thread);
 677     methodHandle m (thread, method(thread));
 678     Bytecode_invoke call(m, bci(thread));
 679     Symbol* signature = call.signature();
 680     receiver = Handle(thread,
 681                   thread->last_frame().interpreter_callee_receiver(signature));
 682     assert(Universe::heap()->is_in_reserved_or_null(receiver()),
 683            "sanity check");
 684     assert(receiver.is_null() ||
 685            !Universe::heap()->is_in_reserved(receiver->klass()),
 686            "sanity check");
 687   }
 688 
 689   // resolve method
 690   CallInfo info;
 691   constantPoolHandle pool(thread, method(thread)->constants());
 692 
 693   {
 694     JvmtiHideSingleStepping jhss(thread);
 695     LinkResolver::resolve_invoke(info, receiver, pool,
 696                                  get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
 697     if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
 698       int retry_count = 0;
 699       while (info.resolved_method()->is_old()) {
 700         // It is very unlikely that method is redefined more than 100 times
 701         // in the middle of resolve. If it is looping here more than 100 times
 702         // means then there could be a bug here.
 703         guarantee((retry_count++ < 100),
 704                   "Could not resolve to latest version of redefined method");
 705         // method is redefined in the middle of resolve so re-try.
 706         LinkResolver::resolve_invoke(info, receiver, pool,
 707                                      get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
 708       }
 709     }
 710   } // end JvmtiHideSingleStepping
 711 
 712   // check if link resolution caused cpCache to be updated
 713   if (already_resolved(thread)) return;
 714 
 715   if (bytecode == Bytecodes::_invokeinterface) {
 716 
 717     if (TraceItables && Verbose) {
 718       ResourceMark rm(thread);
 719       tty->print_cr("Resolving: klass: %s to method: %s", info.resolved_klass()->name()->as_C_string(), info.resolved_method()->name()->as_C_string());
 720     }
 721     if (info.resolved_method()->method_holder() ==
 722                                             SystemDictionary::Object_klass()) {
 723       // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
 724       // (see also cpCacheOop.cpp for details)
 725       methodHandle rm = info.resolved_method();
 726       assert(rm->is_final() || info.has_vtable_index(),
 727              "should have been set already");
 728       cache_entry(thread)->set_method(bytecode, rm, info.vtable_index());
 729     } else {
 730       // Setup itable entry
 731       int index = klassItable::compute_itable_index(info.resolved_method()());
 732       cache_entry(thread)->set_interface_call(info.resolved_method(), index);
 733     }
 734   } else {
 735     cache_entry(thread)->set_method(
 736       bytecode,
 737       info.resolved_method(),
 738       info.vtable_index());
 739   }
 740 }
 741 IRT_END
 742 
 743 
 744 // First time execution:  Resolve symbols, create a permanent MethodType object.
 745 IRT_ENTRY(void, InterpreterRuntime::resolve_invokehandle(JavaThread* thread)) {
 746   assert(EnableInvokeDynamic, "");
 747   const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
 748 
 749   // resolve method
 750   CallInfo info;
 751   constantPoolHandle pool(thread, method(thread)->constants());
 752 
 753   {
 754     JvmtiHideSingleStepping jhss(thread);
 755     LinkResolver::resolve_invoke(info, Handle(), pool,
 756                                  get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
 757   } // end JvmtiHideSingleStepping
 758 
 759   cache_entry(thread)->set_method_handle(pool, info);
 760 }
 761 IRT_END
 762 
 763 
 764 // First time execution:  Resolve symbols, create a permanent CallSite object.
 765 IRT_ENTRY(void, InterpreterRuntime::resolve_invokedynamic(JavaThread* thread)) {
 766   assert(EnableInvokeDynamic, "");
 767   const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
 768 
 769   //TO DO: consider passing BCI to Java.
 770   //  int caller_bci = method(thread)->bci_from(bcp(thread));
 771 
 772   // resolve method
 773   CallInfo info;
 774   constantPoolHandle pool(thread, method(thread)->constants());
 775   int index = get_index_u4(thread, bytecode);
 776   {
 777     JvmtiHideSingleStepping jhss(thread);
 778     LinkResolver::resolve_invoke(info, Handle(), pool,
 779                                  index, bytecode, CHECK);
 780   } // end JvmtiHideSingleStepping
 781 
 782   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
 783   cp_cache_entry->set_dynamic_call(pool, info);
 784 }
 785 IRT_END
 786 
 787 
 788 //------------------------------------------------------------------------------------------------------------------------
 789 // Miscellaneous
 790 
 791 
 792 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) {
 793   nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
 794   assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
 795   if (branch_bcp != NULL && nm != NULL) {
 796     // This was a successful request for an OSR nmethod.  Because
 797     // frequency_counter_overflow_inner ends with a safepoint check,
 798     // nm could have been unloaded so look it up again.  It's unsafe
 799     // to examine nm directly since it might have been freed and used
 800     // for something else.
 801     frame fr = thread->last_frame();
 802     Method* method =  fr.interpreter_frame_method();
 803     int bci = method->bci_from(fr.interpreter_frame_bcp());
 804     nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
 805   }
 806 #ifndef PRODUCT
 807   if (TraceOnStackReplacement) {
 808     if (nm != NULL) {
 809       tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", nm->osr_entry());
 810       nm->print();
 811     }
 812   }
 813 #endif
 814   return nm;
 815 }
 816 
 817 IRT_ENTRY(nmethod*,
 818           InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp))
 819   // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
 820   // flag, in case this method triggers classloading which will call into Java.
 821   UnlockFlagSaver fs(thread);
 822 
 823   frame fr = thread->last_frame();
 824   assert(fr.is_interpreted_frame(), "must come from interpreter");
 825   methodHandle method(thread, fr.interpreter_frame_method());
 826   const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : InvocationEntryBci;
 827   const int bci = branch_bcp != NULL ? method->bci_from(fr.interpreter_frame_bcp()) : InvocationEntryBci;
 828 
 829   assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
 830   nmethod* osr_nm = CompilationPolicy::policy()->event(method, method, branch_bci, bci, CompLevel_none, NULL, thread);
 831   assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
 832 
 833   if (osr_nm != NULL) {
 834     // We may need to do on-stack replacement which requires that no
 835     // monitors in the activation are biased because their
 836     // BasicObjectLocks will need to migrate during OSR. Force
 837     // unbiasing of all monitors in the activation now (even though
 838     // the OSR nmethod might be invalidated) because we don't have a
 839     // safepoint opportunity later once the migration begins.
 840     if (UseBiasedLocking) {
 841       ResourceMark rm;
 842       GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
 843       for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
 844            kptr < fr.interpreter_frame_monitor_begin();
 845            kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
 846         if( kptr->obj() != NULL ) {
 847           objects_to_revoke->append(Handle(THREAD, kptr->obj()));
 848         }
 849       }
 850       BiasedLocking::revoke(objects_to_revoke);
 851     }
 852   }
 853   return osr_nm;
 854 IRT_END
 855 
 856 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
 857   assert(ProfileInterpreter, "must be profiling interpreter");
 858   int bci = method->bci_from(cur_bcp);
 859   MethodData* mdo = method->method_data();
 860   if (mdo == NULL)  return 0;
 861   return mdo->bci_to_di(bci);
 862 IRT_END
 863 
 864 IRT_ENTRY(void, InterpreterRuntime::profile_method(JavaThread* thread))
 865   // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
 866   // flag, in case this method triggers classloading which will call into Java.
 867   UnlockFlagSaver fs(thread);
 868 
 869   assert(ProfileInterpreter, "must be profiling interpreter");
 870   frame fr = thread->last_frame();
 871   assert(fr.is_interpreted_frame(), "must come from interpreter");
 872   methodHandle method(thread, fr.interpreter_frame_method());
 873   Method::build_interpreter_method_data(method, THREAD);
 874   if (HAS_PENDING_EXCEPTION) {
 875     assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
 876     CLEAR_PENDING_EXCEPTION;
 877     // and fall through...
 878   }
 879 IRT_END
 880 
 881 
 882 #ifdef ASSERT
 883 IRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
 884   assert(ProfileInterpreter, "must be profiling interpreter");
 885 
 886   MethodData* mdo = method->method_data();
 887   assert(mdo != NULL, "must not be null");
 888 
 889   int bci = method->bci_from(bcp);
 890 
 891   address mdp2 = mdo->bci_to_dp(bci);
 892   if (mdp != mdp2) {
 893     ResourceMark rm;
 894     ResetNoHandleMark rnm; // In a LEAF entry.
 895     HandleMark hm;
 896     tty->print_cr("FAILED verify : actual mdp %p   expected mdp %p @ bci %d", mdp, mdp2, bci);
 897     int current_di = mdo->dp_to_di(mdp);
 898     int expected_di  = mdo->dp_to_di(mdp2);
 899     tty->print_cr("  actual di %d   expected di %d", current_di, expected_di);
 900     int expected_approx_bci = mdo->data_at(expected_di)->bci();
 901     int approx_bci = -1;
 902     if (current_di >= 0) {
 903       approx_bci = mdo->data_at(current_di)->bci();
 904     }
 905     tty->print_cr("  actual bci is %d  expected bci %d", approx_bci, expected_approx_bci);
 906     mdo->print_on(tty);
 907     method->print_codes();
 908   }
 909   assert(mdp == mdp2, "wrong mdp");
 910 IRT_END
 911 #endif // ASSERT
 912 
 913 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci))
 914   assert(ProfileInterpreter, "must be profiling interpreter");
 915   ResourceMark rm(thread);
 916   HandleMark hm(thread);
 917   frame fr = thread->last_frame();
 918   assert(fr.is_interpreted_frame(), "must come from interpreter");
 919   MethodData* h_mdo = fr.interpreter_frame_method()->method_data();
 920 
 921   // Grab a lock to ensure atomic access to setting the return bci and
 922   // the displacement.  This can block and GC, invalidating all naked oops.
 923   MutexLocker ml(RetData_lock);
 924 
 925   // ProfileData is essentially a wrapper around a derived oop, so we
 926   // need to take the lock before making any ProfileData structures.
 927   ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp()));
 928   RetData* rdata = data->as_RetData();
 929   address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
 930   fr.interpreter_frame_set_mdp(new_mdp);
 931 IRT_END
 932 
 933 IRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* thread, Method* m))
 934   MethodCounters* mcs = Method::build_method_counters(m, thread);
 935   if (HAS_PENDING_EXCEPTION) {
 936     assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
 937     CLEAR_PENDING_EXCEPTION;
 938   }
 939   return mcs;
 940 IRT_END
 941 
 942 
 943 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread))
 944   // We used to need an explict preserve_arguments here for invoke bytecodes. However,
 945   // stack traversal automatically takes care of preserving arguments for invoke, so
 946   // this is no longer needed.
 947 
 948   // IRT_END does an implicit safepoint check, hence we are guaranteed to block
 949   // if this is called during a safepoint
 950 
 951   if (JvmtiExport::should_post_single_step()) {
 952     // We are called during regular safepoints and when the VM is
 953     // single stepping. If any thread is marked for single stepping,
 954     // then we may have JVMTI work to do.
 955     JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread));
 956   }
 957 IRT_END
 958 
 959 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj,
 960 ConstantPoolCacheEntry *cp_entry))
 961 
 962   // check the access_flags for the field in the klass
 963 
 964   InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
 965   int index = cp_entry->field_index();
 966   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
 967 
 968   switch(cp_entry->flag_state()) {
 969     case btos:    // fall through
 970     case ctos:    // fall through
 971     case stos:    // fall through
 972     case itos:    // fall through
 973     case ftos:    // fall through
 974     case ltos:    // fall through
 975     case dtos:    // fall through
 976     case atos: break;
 977     default: ShouldNotReachHere(); return;
 978   }
 979   bool is_static = (obj == NULL);
 980   HandleMark hm(thread);
 981 
 982   Handle h_obj;
 983   if (!is_static) {
 984     // non-static field accessors have an object, but we need a handle
 985     h_obj = Handle(thread, obj);
 986   }
 987   instanceKlassHandle h_cp_entry_f1(thread, (Klass*)cp_entry->f1_as_klass());
 988   jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_cp_entry_f1, cp_entry->f2_as_index(), is_static);
 989   JvmtiExport::post_field_access(thread, method(thread), bcp(thread), h_cp_entry_f1, h_obj, fid);
 990 IRT_END
 991 
 992 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread,
 993   oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value))
 994 
 995   Klass* k = (Klass*)cp_entry->f1_as_klass();
 996 
 997   // check the access_flags for the field in the klass
 998   InstanceKlass* ik = InstanceKlass::cast(k);
 999   int index = cp_entry->field_index();
1000   // bail out if field modifications are not watched
1001   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1002 
1003   char sig_type = '\0';
1004 
1005   switch(cp_entry->flag_state()) {
1006     case btos: sig_type = 'Z'; break;
1007     case ctos: sig_type = 'C'; break;
1008     case stos: sig_type = 'S'; break;
1009     case itos: sig_type = 'I'; break;
1010     case ftos: sig_type = 'F'; break;
1011     case atos: sig_type = 'L'; break;
1012     case ltos: sig_type = 'J'; break;
1013     case dtos: sig_type = 'D'; break;
1014     default:  ShouldNotReachHere(); return;
1015   }
1016   bool is_static = (obj == NULL);
1017 
1018   HandleMark hm(thread);
1019   instanceKlassHandle h_klass(thread, k);
1020   jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_klass, cp_entry->f2_as_index(), is_static);
1021   jvalue fvalue;
1022 #ifdef _LP64
1023   fvalue = *value;
1024 #else
1025   // Long/double values are stored unaligned and also noncontiguously with
1026   // tagged stacks.  We can't just do a simple assignment even in the non-
1027   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1028   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1029   // We assume that the two halves of longs/doubles are stored in interpreter
1030   // stack slots in platform-endian order.
1031   jlong_accessor u;
1032   jint* newval = (jint*)value;
1033   u.words[0] = newval[0];
1034   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1035   fvalue.j = u.long_value;
1036 #endif // _LP64
1037 
1038   Handle h_obj;
1039   if (!is_static) {
1040     // non-static field accessors have an object, but we need a handle
1041     h_obj = Handle(thread, obj);
1042   }
1043 
1044   JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), h_klass, h_obj,
1045                                            fid, sig_type, &fvalue);
1046 IRT_END
1047 
1048 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread))
1049   JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1050 IRT_END
1051 
1052 
1053 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread))
1054   JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1055 IRT_END
1056 
1057 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1058 {
1059   return (Interpreter::contains(pc) ? 1 : 0);
1060 }
1061 IRT_END
1062 
1063 
1064 // Implementation of SignatureHandlerLibrary
1065 
1066 address SignatureHandlerLibrary::set_handler_blob() {
1067   BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1068   if (handler_blob == NULL) {
1069     return NULL;
1070   }
1071   address handler = handler_blob->code_begin();
1072   _handler_blob = handler_blob;
1073   _handler = handler;
1074   return handler;
1075 }
1076 
1077 void SignatureHandlerLibrary::initialize() {
1078   if (_fingerprints != NULL) {
1079     return;
1080   }
1081   if (set_handler_blob() == NULL) {
1082     vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1083   }
1084 
1085   BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1086                                       SignatureHandlerLibrary::buffer_size);
1087   _buffer = bb->code_begin();
1088 
1089   _fingerprints = new(ResourceObj::C_HEAP, mtCode)GrowableArray<uint64_t>(32, true);
1090   _handlers     = new(ResourceObj::C_HEAP, mtCode)GrowableArray<address>(32, true);
1091 }
1092 
1093 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1094   address handler   = _handler;
1095   int     insts_size = buffer->pure_insts_size();
1096   if (handler + insts_size > _handler_blob->code_end()) {
1097     // get a new handler blob
1098     handler = set_handler_blob();
1099   }
1100   if (handler != NULL) {
1101     memcpy(handler, buffer->insts_begin(), insts_size);
1102     pd_set_handler(handler);
1103     ICache::invalidate_range(handler, insts_size);
1104     _handler = handler + insts_size;
1105   }
1106   return handler;
1107 }
1108 
1109 void SignatureHandlerLibrary::add(methodHandle method) {
1110   if (method->signature_handler() == NULL) {
1111     // use slow signature handler if we can't do better
1112     int handler_index = -1;
1113     // check if we can use customized (fast) signature handler
1114     if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) {
1115       // use customized signature handler
1116       MutexLocker mu(SignatureHandlerLibrary_lock);
1117       // make sure data structure is initialized
1118       initialize();
1119       // lookup method signature's fingerprint
1120       uint64_t fingerprint = Fingerprinter(method).fingerprint();
1121       handler_index = _fingerprints->find(fingerprint);
1122       // create handler if necessary
1123       if (handler_index < 0) {
1124         ResourceMark rm;
1125         ptrdiff_t align_offset = (address)
1126           round_to((intptr_t)_buffer, CodeEntryAlignment) - (address)_buffer;
1127         CodeBuffer buffer((address)(_buffer + align_offset),
1128                           SignatureHandlerLibrary::buffer_size - align_offset);
1129         InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1130         // copy into code heap
1131         address handler = set_handler(&buffer);
1132         if (handler == NULL) {
1133           // use slow signature handler
1134         } else {
1135           // debugging suppport
1136           if (PrintSignatureHandlers) {
1137             tty->cr();
1138             tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1139                           _handlers->length(),
1140                           (method->is_static() ? "static" : "receiver"),
1141                           method->name_and_sig_as_C_string(),
1142                           fingerprint,
1143                           buffer.insts_size());
1144             Disassembler::decode(handler, handler + buffer.insts_size());
1145 #ifndef PRODUCT
1146             tty->print_cr(" --- associated result handler ---");
1147             address rh_begin = Interpreter::result_handler(method()->result_type());
1148             address rh_end = rh_begin;
1149             while (*(int*)rh_end != 0) {
1150               rh_end += sizeof(int);
1151             }
1152             Disassembler::decode(rh_begin, rh_end);
1153 #endif
1154           }
1155           // add handler to library
1156           _fingerprints->append(fingerprint);
1157           _handlers->append(handler);
1158           // set handler index
1159           assert(_fingerprints->length() == _handlers->length(), "sanity check");
1160           handler_index = _fingerprints->length() - 1;
1161         }
1162       }
1163       // Set handler under SignatureHandlerLibrary_lock
1164     if (handler_index < 0) {
1165       // use generic signature handler
1166       method->set_signature_handler(Interpreter::slow_signature_handler());
1167     } else {
1168       // set handler
1169       method->set_signature_handler(_handlers->at(handler_index));
1170     }
1171     } else {
1172       CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1173       // use generic signature handler
1174       method->set_signature_handler(Interpreter::slow_signature_handler());
1175     }
1176   }
1177 #ifdef ASSERT
1178   int handler_index = -1;
1179   int fingerprint_index = -2;
1180   {
1181     // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1182     // in any way if accessed from multiple threads. To avoid races with another
1183     // thread which may change the arrays in the above, mutex protected block, we
1184     // have to protect this read access here with the same mutex as well!
1185     MutexLocker mu(SignatureHandlerLibrary_lock);
1186     if (_handlers != NULL) {
1187     handler_index = _handlers->find(method->signature_handler());
1188     fingerprint_index = _fingerprints->find(Fingerprinter(method).fingerprint());
1189   }
1190   }
1191   assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1192          handler_index == fingerprint_index, "sanity check");
1193 #endif // ASSERT
1194 }
1195 
1196 
1197 BufferBlob*              SignatureHandlerLibrary::_handler_blob = NULL;
1198 address                  SignatureHandlerLibrary::_handler      = NULL;
1199 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL;
1200 GrowableArray<address>*  SignatureHandlerLibrary::_handlers     = NULL;
1201 address                  SignatureHandlerLibrary::_buffer       = NULL;
1202 
1203 
1204 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, Method* method))
1205   methodHandle m(thread, method);
1206   assert(m->is_native(), "sanity check");
1207   // lookup native function entry point if it doesn't exist
1208   bool in_base_library;
1209   if (!m->has_native_function()) {
1210     NativeLookup::lookup(m, in_base_library, CHECK);
1211   }
1212   // make sure signature handler is installed
1213   SignatureHandlerLibrary::add(m);
1214   // The interpreter entry point checks the signature handler first,
1215   // before trying to fetch the native entry point and klass mirror.
1216   // We must set the signature handler last, so that multiple processors
1217   // preparing the same method will be sure to see non-null entry & mirror.
1218 IRT_END
1219 
1220 #if defined(IA32) || defined(AMD64) || defined(ARM)
1221 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address))
1222   if (src_address == dest_address) {
1223     return;
1224   }
1225   ResetNoHandleMark rnm; // In a LEAF entry.
1226   HandleMark hm;
1227   ResourceMark rm;
1228   frame fr = thread->last_frame();
1229   assert(fr.is_interpreted_frame(), "");
1230   jint bci = fr.interpreter_frame_bci();
1231   methodHandle mh(thread, fr.interpreter_frame_method());
1232   Bytecode_invoke invoke(mh, bci);
1233   ArgumentSizeComputer asc(invoke.signature());
1234   int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1235   Copy::conjoint_jbytes(src_address, dest_address,
1236                        size_of_arguments * Interpreter::stackElementSize);
1237 IRT_END
1238 #endif
1239 
1240 #if INCLUDE_JVMTI
1241 // This is a support of the JVMTI PopFrame interface.
1242 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1243 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters.
1244 // The dmh argument is a reference to a DirectMethoHandle that has a member name field.
1245 IRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* thread, address dmh,
1246                                                             Method* method, address bcp))
1247   Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1248   if (code != Bytecodes::_invokestatic) {
1249     return;
1250   }
1251   ConstantPool* cpool = method->constants();
1252   int cp_index = Bytes::get_native_u2(bcp + 1) + ConstantPool::CPCACHE_INDEX_TAG;
1253   Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index));
1254   Symbol* mname = cpool->name_ref_at(cp_index);
1255 
1256   if (MethodHandles::has_member_arg(cname, mname)) {
1257     oop member_name = java_lang_invoke_DirectMethodHandle::member((oop)dmh);
1258     thread->set_vm_result(member_name);
1259   }
1260 IRT_END
1261 #endif // INCLUDE_JVMTI