src/share/vm/interpreter/interpreterRuntime.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7419
d3f3f7677537
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial