< prev index next >

src/hotspot/share/interpreter/interpreterRuntime.cpp

Print this page

   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 "jvm_io.h"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/symbolTable.hpp"

  29 #include "classfile/vmClasses.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "compiler/compilationPolicy.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "compiler/disassembler.hpp"
  35 #include "gc/shared/barrierSetNMethod.hpp"
  36 #include "gc/shared/collectedHeap.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "interpreter/interpreterRuntime.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "interpreter/templateTable.hpp"
  41 #include "logging/log.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/constantPool.hpp"
  46 #include "oops/cpCache.inline.hpp"



  47 #include "oops/instanceKlass.inline.hpp"
  48 #include "oops/klass.inline.hpp"
  49 #include "oops/methodData.hpp"
  50 #include "oops/objArrayKlass.hpp"
  51 #include "oops/objArrayOop.inline.hpp"
  52 #include "oops/oop.inline.hpp"
  53 #include "oops/symbol.hpp"
  54 #include "prims/jvmtiExport.hpp"
  55 #include "prims/methodHandles.hpp"
  56 #include "prims/nativeLookup.hpp"
  57 #include "runtime/atomic.hpp"
  58 #include "runtime/deoptimization.hpp"
  59 #include "runtime/fieldDescriptor.inline.hpp"
  60 #include "runtime/frame.inline.hpp"
  61 #include "runtime/handles.inline.hpp"
  62 #include "runtime/icache.hpp"
  63 #include "runtime/interfaceSupport.inline.hpp"
  64 #include "runtime/java.hpp"
  65 #include "runtime/javaCalls.hpp"
  66 #include "runtime/jfieldIDWorkaround.hpp"
  67 #include "runtime/osThread.hpp"
  68 #include "runtime/sharedRuntime.hpp"
  69 #include "runtime/stackWatermarkSet.hpp"
  70 #include "runtime/stubRoutines.hpp"
  71 #include "runtime/synchronizer.hpp"
  72 #include "runtime/threadCritical.hpp"
  73 #include "utilities/align.hpp"
  74 #include "utilities/copy.hpp"
  75 #include "utilities/events.hpp"

  76 #ifdef COMPILER2
  77 #include "opto/runtime.hpp"
  78 #endif
  79 
  80 // Helper class to access current interpreter state
  81 class LastFrameAccessor : public StackObj {
  82   frame _last_frame;
  83 public:
  84   LastFrameAccessor(JavaThread* current) {
  85     assert(current == Thread::current(), "sanity");
  86     _last_frame = current->last_frame();
  87   }
  88   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  89   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  90   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  91   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  92   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
  93 
  94   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
  95   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

 137     if (mdo != NULL) {
 138       NEEDS_CLEANUP;
 139       last_frame.set_mdp(mdo->bci_to_dp(last_frame.bci()));
 140     }
 141   }
 142 }
 143 
 144 //------------------------------------------------------------------------------------------------------------------------
 145 // Constants
 146 
 147 
 148 JRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* current, bool wide))
 149   // access constant pool
 150   LastFrameAccessor last_frame(current);
 151   ConstantPool* pool = last_frame.method()->constants();
 152   int index = wide ? last_frame.get_index_u2(Bytecodes::_ldc_w) : last_frame.get_index_u1(Bytecodes::_ldc);
 153   constantTag tag = pool->tag_at(index);
 154 
 155   assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
 156   Klass* klass = pool->klass_at(index, CHECK);
 157   oop java_class = klass->java_mirror();


 158   current->set_vm_result(java_class);
 159 JRT_END
 160 
 161 JRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* current, Bytecodes::Code bytecode)) {
 162   assert(bytecode == Bytecodes::_ldc ||
 163          bytecode == Bytecodes::_ldc_w ||
 164          bytecode == Bytecodes::_ldc2_w ||
 165          bytecode == Bytecodes::_fast_aldc ||
 166          bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
 167   ResourceMark rm(current);
 168   const bool is_fast_aldc = (bytecode == Bytecodes::_fast_aldc ||
 169                              bytecode == Bytecodes::_fast_aldc_w);
 170   LastFrameAccessor last_frame(current);
 171   methodHandle m (current, last_frame.method());
 172   Bytecode_loadconstant ldc(m, last_frame.bci());
 173 
 174   // Double-check the size.  (Condy can have any type.)
 175   BasicType type = ldc.result_type();
 176   switch (type2size[type]) {
 177   case 2: guarantee(bytecode == Bytecodes::_ldc2_w, ""); break;

 201   current->set_vm_result(result);
 202   if (!is_fast_aldc) {
 203     // Tell the interpreter how to unbox the primitive.
 204     guarantee(java_lang_boxing_object::is_instance(result, type), "");
 205     int offset = java_lang_boxing_object::value_offset(type);
 206     intptr_t flags = ((as_TosState(type) << ConstantPoolCacheEntry::tos_state_shift)
 207                       | (offset & ConstantPoolCacheEntry::field_index_mask));
 208     current->set_vm_result_2((Metadata*)flags);
 209   }
 210 }
 211 JRT_END
 212 
 213 
 214 //------------------------------------------------------------------------------------------------------------------------
 215 // Allocation
 216 
 217 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 218   Klass* k = pool->klass_at(index, CHECK);
 219   InstanceKlass* klass = InstanceKlass::cast(k);
 220 




 221   // Make sure we are not instantiating an abstract klass
 222   klass->check_valid_for_instantiation(true, CHECK);
 223 
 224   // Make sure klass is initialized
 225   klass->initialize(CHECK);
 226 
 227   // At this point the class may not be fully initialized
 228   // because of recursive initialization. If it is fully
 229   // initialized & has_finalized is not set, we rewrite
 230   // it into its fast version (Note: no locking is needed
 231   // here since this is an atomic byte write and can be
 232   // done more than once).
 233   //
 234   // Note: In case of classes with has_finalized we don't
 235   //       rewrite since that saves us an extra check in
 236   //       the fast version which then would call the
 237   //       slow version anyway (and do a call back into
 238   //       Java).
 239   //       If we have a breakpoint, then we don't rewrite
 240   //       because the _breakpoint bytecode would be lost.
 241   oop obj = klass->allocate_instance(CHECK);
 242   current->set_vm_result(obj);
 243 JRT_END
 244 


































































































































































































 245 
 246 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 247   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 248   current->set_vm_result(obj);
 249 JRT_END
 250 
 251 
 252 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 253   Klass*    klass = pool->klass_at(index, CHECK);
 254   objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);







 255   current->set_vm_result(obj);
 256 JRT_END
 257 










 258 
 259 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 260   // We may want to pass in more arguments - could make this slightly faster
 261   LastFrameAccessor last_frame(current);
 262   ConstantPool* constants = last_frame.method()->constants();
 263   int          i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 264   Klass* klass   = constants->klass_at(i, CHECK);

 265   int   nof_dims = last_frame.number_of_dimensions();
 266   assert(klass->is_klass(), "not a class");
 267   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 268 




 269   // We must create an array of jints to pass to multi_allocate.
 270   ResourceMark rm(current);
 271   const int small_dims = 10;
 272   jint dim_array[small_dims];
 273   jint *dims = &dim_array[0];
 274   if (nof_dims > small_dims) {
 275     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 276   }
 277   for (int index = 0; index < nof_dims; index++) {
 278     // offset from first_size_address is addressed as local[index]
 279     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 280     dims[index] = first_size_address[n];
 281   }
 282   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 283   current->set_vm_result(obj);
 284 JRT_END
 285 
 286 
 287 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 288   assert(oopDesc::is_oop(obj), "must be a valid oop");
 289   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 290   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 291 JRT_END
 292 























 293 
 294 // Quicken instance-of and check-cast bytecodes
 295 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 296   // Force resolving; quicken the bytecode
 297   LastFrameAccessor last_frame(current);
 298   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 299   ConstantPool* cpool = last_frame.method()->constants();
 300   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 301   // program we might have seen an unquick'd bytecode in the interpreter but have another
 302   // thread quicken the bytecode before we get here.
 303   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 304   Klass* klass = cpool->klass_at(which, CHECK);
 305   current->set_vm_result_2(klass);
 306 JRT_END
 307 
 308 
 309 //------------------------------------------------------------------------------------------------------------------------
 310 // Exceptions
 311 
 312 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 599 // and therefore we don't have the receiver object at our fingertips. (Though,
 600 // on some platforms the receiver still resides in a register...). Thus,
 601 // we have no choice but print an error message not containing the receiver
 602 // type.
 603 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 604                                                                         Method* missingMethod))
 605   ResourceMark rm(current);
 606   assert(missingMethod != NULL, "sanity");
 607   methodHandle m(current, missingMethod);
 608   LinkResolver::throw_abstract_method_error(m, THREAD);
 609 JRT_END
 610 
 611 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 612                                                                      Klass* recvKlass,
 613                                                                      Method* missingMethod))
 614   ResourceMark rm(current);
 615   methodHandle mh = methodHandle(current, missingMethod);
 616   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 617 JRT_END
 618 




 619 
 620 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 621   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 622 JRT_END
 623 
 624 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 625                                                                               Klass* recvKlass,
 626                                                                               Klass* interfaceKlass))
 627   ResourceMark rm(current);
 628   char buf[1000];
 629   buf[0] = '\0';
 630   jio_snprintf(buf, sizeof(buf),
 631                "Class %s does not implement the requested interface %s",
 632                recvKlass ? recvKlass->external_name() : "NULL",
 633                interfaceKlass ? interfaceKlass->external_name() : "NULL");
 634   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 635 JRT_END
 636 
 637 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 638   THROW(vmSymbols::java_lang_NullPointerException());
 639 JRT_END
 640 
 641 //------------------------------------------------------------------------------------------------------------------------
 642 // Fields
 643 //
 644 
 645 void InterpreterRuntime::resolve_get_put(JavaThread* current, Bytecodes::Code bytecode) {
 646   // resolve field
 647   fieldDescriptor info;
 648   LastFrameAccessor last_frame(current);
 649   constantPoolHandle pool(current, last_frame.method()->constants());
 650   methodHandle m(current, last_frame.method());
 651   bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_nofast_putfield ||
 652                     bytecode == Bytecodes::_putstatic);
 653   bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);

 654 
 655   {
 656     JvmtiHideSingleStepping jhss(current);
 657     JavaThread* THREAD = current; // For exception macros.
 658     LinkResolver::resolve_field_access(info, pool, last_frame.get_index_u2_cpcache(bytecode),
 659                                        m, bytecode, CHECK);
 660   } // end JvmtiHideSingleStepping
 661 
 662   // check if link resolution caused cpCache to be updated
 663   ConstantPoolCacheEntry* cp_cache_entry = last_frame.cache_entry();
 664   if (cp_cache_entry->is_resolved(bytecode)) return;
 665 
 666   // compute auxiliary field attributes
 667   TosState state  = as_TosState(info.field_type());
 668 
 669   // Resolution of put instructions on final fields is delayed. That is required so that
 670   // exceptions are thrown at the correct place (when the instruction is actually invoked).
 671   // If we do not resolve an instruction in the current pass, leaving the put_code
 672   // set to zero will cause the next put instruction to the same field to reresolve.
 673 

 678   // initializer method <init>. If resolution were not inhibited, a putfield
 679   // in an initializer method could be resolved in the initializer. Subsequent
 680   // putfield instructions to the same field would then use cached information.
 681   // As a result, those instructions would not pass through the VM. That is,
 682   // checks in resolve_field_access() would not be executed for those instructions
 683   // and the required IllegalAccessError would not be thrown.
 684   //
 685   // Also, we need to delay resolving getstatic and putstatic instructions until the
 686   // class is initialized.  This is required so that access to the static
 687   // field will call the initialization function every time until the class
 688   // is completely initialized ala. in 2.17.5 in JVM Specification.
 689   InstanceKlass* klass = info.field_holder();
 690   bool uninitialized_static = is_static && !klass->is_initialized();
 691   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 692                                       info.has_initialized_final_update();
 693   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 694 
 695   Bytecodes::Code get_code = (Bytecodes::Code)0;
 696   Bytecodes::Code put_code = (Bytecodes::Code)0;
 697   if (!uninitialized_static) {
 698     get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
 699     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 700       put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);






 701     }
 702   }
 703 
 704   cp_cache_entry->set_field(
 705     get_code,
 706     put_code,
 707     info.field_holder(),
 708     info.index(),
 709     info.offset(),
 710     state,
 711     info.access_flags().is_final(),
 712     info.access_flags().is_volatile()


 713   );
 714 }
 715 
 716 
 717 //------------------------------------------------------------------------------------------------------------------------
 718 // Synchronization
 719 //
 720 // The interpreter's synchronization code is factored out so that it can
 721 // be shared by method invocation and synchronized blocks.
 722 //%note synchronization_3
 723 
 724 //%note monitor_1
 725 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 726 #ifdef ASSERT
 727   current->last_frame().interpreter_frame_verify_monitor(elem);
 728 #endif
 729   Handle h_obj(current, elem->obj());
 730   assert(Universe::heap()->is_in_or_null(h_obj()),
 731          "must be NULL or an object");
 732   ObjectSynchronizer::enter(h_obj, elem->lock(), current);

 924   {
 925     JvmtiHideSingleStepping jhss(current);
 926     JavaThread* THREAD = current; // For exception macros.
 927     LinkResolver::resolve_invoke(info, Handle(), pool,
 928                                  index, bytecode, CHECK);
 929   } // end JvmtiHideSingleStepping
 930 
 931   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
 932   cp_cache_entry->set_dynamic_call(pool, info);
 933 }
 934 
 935 // This function is the interface to the assembly code. It returns the resolved
 936 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
 937 // This function will check for redefinition!
 938 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
 939   switch (bytecode) {
 940   case Bytecodes::_getstatic:
 941   case Bytecodes::_putstatic:
 942   case Bytecodes::_getfield:
 943   case Bytecodes::_putfield:

 944     resolve_get_put(current, bytecode);
 945     break;
 946   case Bytecodes::_invokevirtual:
 947   case Bytecodes::_invokespecial:
 948   case Bytecodes::_invokestatic:
 949   case Bytecodes::_invokeinterface:
 950     resolve_invoke(current, bytecode);
 951     break;
 952   case Bytecodes::_invokehandle:
 953     resolve_invokehandle(current);
 954     break;
 955   case Bytecodes::_invokedynamic:
 956     resolve_invokedynamic(current);
 957     break;
 958   default:
 959     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
 960     break;
 961   }
 962 }
 963 JRT_END

1121   // This function is called by the interpreter when the return poll found a reason
1122   // to call the VM. The reason could be that we are returning into a not yet safe
1123   // to access frame. We handle that below.
1124   // Note that this path does not check for single stepping, because we do not want
1125   // to single step when unwinding frames for an exception being thrown. Instead,
1126   // such single stepping code will use the safepoint table, which will use the
1127   // InterpreterRuntime::at_safepoint callback.
1128   StackWatermarkSet::before_unwind(current);
1129 JRT_END
1130 
1131 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1132                                                       ConstantPoolCacheEntry *cp_entry))
1133 
1134   // check the access_flags for the field in the klass
1135 
1136   InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1137   int index = cp_entry->field_index();
1138   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1139 
1140   bool is_static = (obj == NULL);

1141   HandleMark hm(current);
1142 
1143   Handle h_obj;
1144   if (!is_static) {
1145     // non-static field accessors have an object, but we need a handle
1146     h_obj = Handle(current, obj);
1147   }
1148   InstanceKlass* cp_entry_f1 = InstanceKlass::cast(cp_entry->f1_as_klass());
1149   jfieldID fid = jfieldIDWorkaround::to_jfieldID(cp_entry_f1, cp_entry->f2_as_index(), is_static);
1150   LastFrameAccessor last_frame(current);
1151   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), cp_entry_f1, h_obj, fid);
1152 JRT_END
1153 
1154 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1155                                                             ConstantPoolCacheEntry *cp_entry, jvalue *value))
1156 
1157   Klass* k = cp_entry->f1_as_klass();
1158 
1159   // check the access_flags for the field in the klass
1160   InstanceKlass* ik = InstanceKlass::cast(k);
1161   int index = cp_entry->field_index();
1162   // bail out if field modifications are not watched
1163   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1164 
1165   char sig_type = '\0';
1166 
1167   switch(cp_entry->flag_state()) {
1168     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1169     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1170     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1171     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1172     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1173     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1174     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1175     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1176     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1177     default:  ShouldNotReachHere(); return;
1178   }






1179   bool is_static = (obj == NULL);

1180 
1181   HandleMark hm(current);
1182   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static);
1183   jvalue fvalue;
1184 #ifdef _LP64
1185   fvalue = *value;
1186 #else
1187   // Long/double values are stored unaligned and also noncontiguously with
1188   // tagged stacks.  We can't just do a simple assignment even in the non-
1189   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1190   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1191   // We assume that the two halves of longs/doubles are stored in interpreter
1192   // stack slots in platform-endian order.
1193   jlong_accessor u;
1194   jint* newval = (jint*)value;
1195   u.words[0] = newval[0];
1196   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1197   fvalue.j = u.long_value;
1198 #endif // _LP64
1199 
1200   Handle h_obj;
1201   if (!is_static) {
1202     // non-static field accessors have an object, but we need a handle

   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 "jvm_io.h"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/codeCache.hpp"
  33 #include "compiler/compilationPolicy.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/disassembler.hpp"
  36 #include "gc/shared/barrierSetNMethod.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "interpreter/interpreter.hpp"
  39 #include "interpreter/interpreterRuntime.hpp"
  40 #include "interpreter/linkResolver.hpp"
  41 #include "interpreter/templateTable.hpp"
  42 #include "logging/log.hpp"
  43 #include "memory/oopFactory.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/constantPool.hpp"
  47 #include "oops/cpCache.inline.hpp"
  48 #include "oops/flatArrayKlass.hpp"
  49 #include "oops/flatArrayOop.inline.hpp"
  50 #include "oops/inlineKlass.inline.hpp"
  51 #include "oops/instanceKlass.inline.hpp"
  52 #include "oops/klass.inline.hpp"
  53 #include "oops/methodData.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "oops/symbol.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "prims/methodHandles.hpp"
  60 #include "prims/nativeLookup.hpp"
  61 #include "runtime/atomic.hpp"
  62 #include "runtime/deoptimization.hpp"
  63 #include "runtime/fieldDescriptor.inline.hpp"
  64 #include "runtime/frame.inline.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/icache.hpp"
  67 #include "runtime/interfaceSupport.inline.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/jfieldIDWorkaround.hpp"
  71 #include "runtime/osThread.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/stackWatermarkSet.hpp"
  74 #include "runtime/stubRoutines.hpp"
  75 #include "runtime/synchronizer.hpp"
  76 #include "runtime/threadCritical.hpp"
  77 #include "utilities/align.hpp"
  78 #include "utilities/copy.hpp"
  79 #include "utilities/events.hpp"
  80 #include "utilities/globalDefinitions.hpp"
  81 #ifdef COMPILER2
  82 #include "opto/runtime.hpp"
  83 #endif
  84 
  85 // Helper class to access current interpreter state
  86 class LastFrameAccessor : public StackObj {
  87   frame _last_frame;
  88 public:
  89   LastFrameAccessor(JavaThread* current) {
  90     assert(current == Thread::current(), "sanity");
  91     _last_frame = current->last_frame();
  92   }
  93   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  94   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  95   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  96   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  97   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
  98 
  99   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 100   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

 142     if (mdo != NULL) {
 143       NEEDS_CLEANUP;
 144       last_frame.set_mdp(mdo->bci_to_dp(last_frame.bci()));
 145     }
 146   }
 147 }
 148 
 149 //------------------------------------------------------------------------------------------------------------------------
 150 // Constants
 151 
 152 
 153 JRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* current, bool wide))
 154   // access constant pool
 155   LastFrameAccessor last_frame(current);
 156   ConstantPool* pool = last_frame.method()->constants();
 157   int index = wide ? last_frame.get_index_u2(Bytecodes::_ldc_w) : last_frame.get_index_u1(Bytecodes::_ldc);
 158   constantTag tag = pool->tag_at(index);
 159 
 160   assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
 161   Klass* klass = pool->klass_at(index, CHECK);
 162   oop java_class = tag.is_Qdescriptor_klass()
 163                       ? InlineKlass::cast(klass)->val_mirror()
 164                       : klass->java_mirror();
 165   current->set_vm_result(java_class);
 166 JRT_END
 167 
 168 JRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* current, Bytecodes::Code bytecode)) {
 169   assert(bytecode == Bytecodes::_ldc ||
 170          bytecode == Bytecodes::_ldc_w ||
 171          bytecode == Bytecodes::_ldc2_w ||
 172          bytecode == Bytecodes::_fast_aldc ||
 173          bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
 174   ResourceMark rm(current);
 175   const bool is_fast_aldc = (bytecode == Bytecodes::_fast_aldc ||
 176                              bytecode == Bytecodes::_fast_aldc_w);
 177   LastFrameAccessor last_frame(current);
 178   methodHandle m (current, last_frame.method());
 179   Bytecode_loadconstant ldc(m, last_frame.bci());
 180 
 181   // Double-check the size.  (Condy can have any type.)
 182   BasicType type = ldc.result_type();
 183   switch (type2size[type]) {
 184   case 2: guarantee(bytecode == Bytecodes::_ldc2_w, ""); break;

 208   current->set_vm_result(result);
 209   if (!is_fast_aldc) {
 210     // Tell the interpreter how to unbox the primitive.
 211     guarantee(java_lang_boxing_object::is_instance(result, type), "");
 212     int offset = java_lang_boxing_object::value_offset(type);
 213     intptr_t flags = ((as_TosState(type) << ConstantPoolCacheEntry::tos_state_shift)
 214                       | (offset & ConstantPoolCacheEntry::field_index_mask));
 215     current->set_vm_result_2((Metadata*)flags);
 216   }
 217 }
 218 JRT_END
 219 
 220 
 221 //------------------------------------------------------------------------------------------------------------------------
 222 // Allocation
 223 
 224 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 225   Klass* k = pool->klass_at(index, CHECK);
 226   InstanceKlass* klass = InstanceKlass::cast(k);
 227 
 228   if (klass->is_inline_klass()) {
 229     THROW(vmSymbols::java_lang_InstantiationError());
 230   }
 231 
 232   // Make sure we are not instantiating an abstract klass
 233   klass->check_valid_for_instantiation(true, CHECK);
 234 
 235   // Make sure klass is initialized
 236   klass->initialize(CHECK);
 237 
 238   // At this point the class may not be fully initialized
 239   // because of recursive initialization. If it is fully
 240   // initialized & has_finalized is not set, we rewrite
 241   // it into its fast version (Note: no locking is needed
 242   // here since this is an atomic byte write and can be
 243   // done more than once).
 244   //
 245   // Note: In case of classes with has_finalized we don't
 246   //       rewrite since that saves us an extra check in
 247   //       the fast version which then would call the
 248   //       slow version anyway (and do a call back into
 249   //       Java).
 250   //       If we have a breakpoint, then we don't rewrite
 251   //       because the _breakpoint bytecode would be lost.
 252   oop obj = klass->allocate_instance(CHECK);
 253   current->set_vm_result(obj);
 254 JRT_END
 255 
 256 JRT_ENTRY(void, InterpreterRuntime::defaultvalue(JavaThread* current, ConstantPool* pool, int index))
 257   // Getting the InlineKlass
 258   Klass* k = pool->klass_at(index, CHECK);
 259   if (!k->is_inline_klass()) {
 260     // inconsistency with 'new' which throws an InstantiationError
 261     // in the future, defaultvalue will just return null instead of throwing an exception
 262     THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 263   }
 264   assert(k->is_inline_klass(), "defaultvalue argument must be the inline type class");
 265   InlineKlass* vklass = InlineKlass::cast(k);
 266 
 267   vklass->initialize(CHECK);
 268   oop res = vklass->default_value();
 269   current->set_vm_result(res);
 270 JRT_END
 271 
 272 JRT_ENTRY(int, InterpreterRuntime::withfield(JavaThread* current, ConstantPoolCacheEntry* cpe, uintptr_t ptr))
 273   oop obj = NULL;
 274   int recv_offset = type2size[as_BasicType(cpe->flag_state())];
 275   assert(frame::interpreter_frame_expression_stack_direction() == -1, "currently is -1 on all platforms");
 276   int ret_adj = (recv_offset + type2size[T_OBJECT] )* AbstractInterpreter::stackElementSize;
 277   int offset = cpe->f2_as_offset();
 278   obj = (oopDesc*)(((uintptr_t*)ptr)[recv_offset * Interpreter::stackElementWords]);
 279   if (obj == NULL) {
 280     THROW_(vmSymbols::java_lang_NullPointerException(), ret_adj);
 281   }
 282   assert(oopDesc::is_oop(obj), "Verifying receiver");
 283   assert(obj->klass()->is_inline_klass(), "Must have been checked during resolution");
 284   instanceHandle old_value_h(THREAD, (instanceOop)obj);
 285   oop ref = NULL;
 286   if (cpe->flag_state() == atos) {
 287     ref = *(oopDesc**)ptr;
 288   }
 289   Handle ref_h(THREAD, ref);
 290   InlineKlass* ik = InlineKlass::cast(old_value_h()->klass());
 291   // Ensure that the class is initialized or being initialized
 292   // If the class is in error state, the creation of a new value should not be allowed
 293   ik->initialize(CHECK_(ret_adj));
 294 
 295   bool can_skip = false;
 296   switch(cpe->flag_state()) {
 297     case ztos:
 298       if (old_value_h()->bool_field(offset) == (jboolean)(*(jint*)ptr)) can_skip = true;
 299       break;
 300     case btos:
 301       if (old_value_h()->byte_field(offset) == (jbyte)(*(jint*)ptr)) can_skip = true;
 302       break;
 303     case ctos:
 304       if (old_value_h()->char_field(offset) == (jchar)(*(jint*)ptr)) can_skip = true;
 305       break;
 306     case stos:
 307       if (old_value_h()->short_field(offset) == (jshort)(*(jint*)ptr)) can_skip = true;
 308       break;
 309     case itos:
 310       if (old_value_h()->int_field(offset) == *(jint*)ptr) can_skip = true;
 311       break;
 312     case ltos:
 313       if (old_value_h()->long_field(offset) == *(jlong*)ptr) can_skip = true;
 314       break;
 315     case ftos:
 316       if (memcmp(old_value_h()->field_addr<jfloat>(offset), (jfloat*)ptr, sizeof(jfloat)) == 0) can_skip = true;
 317       break;
 318     case dtos:
 319       if (memcmp(old_value_h()->field_addr<jdouble>(offset), (jdouble*)ptr, sizeof(jdouble)) == 0) can_skip = true;
 320       break;
 321     case atos:
 322       if (!cpe->is_inlined() && old_value_h()->obj_field(offset) == ref_h()) can_skip = true;
 323       break;
 324     default:
 325       break;
 326   }
 327   if (can_skip) {
 328     current->set_vm_result(old_value_h());
 329     return ret_adj;
 330   }
 331 
 332   instanceOop new_value = ik->allocate_instance_buffer(CHECK_(ret_adj));
 333   Handle new_value_h = Handle(THREAD, new_value);
 334   ik->inline_copy_oop_to_new_oop(old_value_h(), new_value_h());
 335   switch(cpe->flag_state()) {
 336     case ztos:
 337       new_value_h()->bool_field_put(offset, (jboolean)(*(jint*)ptr));
 338       break;
 339     case btos:
 340       new_value_h()->byte_field_put(offset, (jbyte)(*(jint*)ptr));
 341       break;
 342     case ctos:
 343       new_value_h()->char_field_put(offset, (jchar)(*(jint*)ptr));
 344       break;
 345     case stos:
 346       new_value_h()->short_field_put(offset, (jshort)(*(jint*)ptr));
 347       break;
 348     case itos:
 349       new_value_h()->int_field_put(offset, (*(jint*)ptr));
 350       break;
 351     case ltos:
 352       new_value_h()->long_field_put(offset, *(jlong*)ptr);
 353       break;
 354     case ftos:
 355       new_value_h()->float_field_put(offset, *(jfloat*)ptr);
 356       break;
 357     case dtos:
 358       new_value_h()->double_field_put(offset, *(jdouble*)ptr);
 359       break;
 360     case atos:
 361       {
 362         if (cpe->is_null_free_inline_type())  {
 363           if (!cpe->is_inlined()) {
 364               if (ref_h() == NULL) {
 365                 THROW_(vmSymbols::java_lang_NullPointerException(), ret_adj);
 366               }
 367               new_value_h()->obj_field_put(offset, ref_h());
 368             } else {
 369               int field_index = cpe->field_index();
 370               InlineKlass* field_ik = InlineKlass::cast(ik->get_inline_type_field_klass(field_index));
 371               field_ik->write_inlined_field(new_value_h(), offset, ref_h(), CHECK_(ret_adj));
 372             }
 373         } else {
 374           new_value_h()->obj_field_put(offset, ref_h());
 375         }
 376       }
 377       break;
 378     default:
 379       ShouldNotReachHere();
 380   }
 381   current->set_vm_result(new_value_h());
 382   return ret_adj;
 383 JRT_END
 384 
 385 JRT_ENTRY(void, InterpreterRuntime::uninitialized_static_inline_type_field(JavaThread* current, oopDesc* mirror, int index))
 386   // The interpreter tries to access an inline static field that has not been initialized.
 387   // This situation can happen in different scenarios:
 388   //   1 - if the load or initialization of the field failed during step 8 of
 389   //       the initialization of the holder of the field, in this case the access to the field
 390   //       must fail
 391   //   2 - it can also happen when the initialization of the holder class triggered the initialization of
 392   //       another class which accesses this field in its static initializer, in this case the
 393   //       access must succeed to allow circularity
 394   // The code below tries to load and initialize the field's class again before returning the default value.
 395   // If the field was not initialized because of an error, a exception should be thrown.
 396   // If the class is being initialized, the default value is returned.
 397   instanceHandle mirror_h(THREAD, (instanceOop)mirror);
 398   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
 399   assert(klass->field_signature(index)->is_Q_signature(), "Sanity check");
 400   if (klass->is_being_initialized() && klass->is_reentrant_initialization(THREAD)) {
 401     int offset = klass->field_offset(index);
 402     Klass* field_k = klass->get_inline_type_field_klass_or_null(index);
 403     if (field_k == NULL) {
 404       field_k = SystemDictionary::resolve_or_fail(klass->field_signature(index)->fundamental_name(THREAD),
 405           Handle(THREAD, klass->class_loader()),
 406           Handle(THREAD, klass->protection_domain()),
 407           true, CHECK);
 408       assert(field_k != NULL, "Should have been loaded or an exception thrown above");
 409       klass->set_inline_type_field_klass(index, field_k);
 410     }
 411     field_k->initialize(CHECK);
 412     oop defaultvalue = InlineKlass::cast(field_k)->default_value();
 413     // It is safe to initialized the static field because 1) the current thread is the initializing thread
 414     // and is the only one that can access it, and 2) the field is actually not initialized (i.e. null)
 415     // otherwise the JVM should not be executing this code.
 416     mirror->obj_field_put(offset, defaultvalue);
 417     current->set_vm_result(defaultvalue);
 418   } else {
 419     assert(klass->is_in_error_state(), "If not initializing, initialization must have failed to get there");
 420     ResourceMark rm(THREAD);
 421     const char* desc = "Could not initialize class ";
 422     const char* className = klass->external_name();
 423     size_t msglen = strlen(desc) + strlen(className) + 1;
 424     char* message = NEW_RESOURCE_ARRAY(char, msglen);
 425     if (NULL == message) {
 426       // Out of memory: can't create detailed error message
 427       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
 428     } else {
 429       jio_snprintf(message, msglen, "%s%s", desc, className);
 430       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
 431     }
 432   }
 433 JRT_END
 434 
 435 JRT_ENTRY(void, InterpreterRuntime::read_inlined_field(JavaThread* current, oopDesc* obj, int index, Klass* field_holder))
 436   Handle obj_h(THREAD, obj);
 437 
 438   assert(oopDesc::is_oop(obj), "Sanity check");
 439 
 440   assert(field_holder->is_instance_klass(), "Sanity check");
 441   InstanceKlass* klass = InstanceKlass::cast(field_holder);
 442 
 443   assert(klass->field_is_inlined(index), "Sanity check");
 444 
 445   InlineKlass* field_vklass = InlineKlass::cast(klass->get_inline_type_field_klass(index));
 446 
 447   oop res = field_vklass->read_inlined_field(obj_h(), klass->field_offset(index), CHECK);
 448   current->set_vm_result(res);
 449 JRT_END
 450 
 451 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 452   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 453   current->set_vm_result(obj);
 454 JRT_END
 455 
 456 
 457 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 458   Klass*    klass = pool->klass_at(index, CHECK);
 459   bool      is_qtype_desc = pool->tag_at(index).is_Qdescriptor_klass();
 460   arrayOop obj;
 461   if ((!klass->is_array_klass()) && is_qtype_desc) { // Logically creates elements, ensure klass init
 462     klass->initialize(CHECK);
 463     obj = oopFactory::new_valueArray(klass, size, CHECK);
 464   } else {
 465     obj = oopFactory::new_objArray(klass, size, CHECK);
 466   }
 467   current->set_vm_result(obj);
 468 JRT_END
 469 
 470 JRT_ENTRY(void, InterpreterRuntime::value_array_load(JavaThread* current, arrayOopDesc* array, int index))
 471   flatArrayHandle vah(current, (flatArrayOop)array);
 472   oop value_holder = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, CHECK);
 473   current->set_vm_result(value_holder);
 474 JRT_END
 475 
 476 JRT_ENTRY(void, InterpreterRuntime::value_array_store(JavaThread* current, void* val, arrayOopDesc* array, int index))
 477   assert(val != NULL, "can't store null into flat array");
 478   ((flatArrayOop)array)->value_copy_to_index(cast_to_oop(val), index);
 479 JRT_END
 480 
 481 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 482   // We may want to pass in more arguments - could make this slightly faster
 483   LastFrameAccessor last_frame(current);
 484   ConstantPool* constants = last_frame.method()->constants();
 485   int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 486   Klass* klass = constants->klass_at(i, CHECK);
 487   bool is_qtype = klass->name()->is_Q_array_signature();
 488   int   nof_dims = last_frame.number_of_dimensions();
 489   assert(klass->is_klass(), "not a class");
 490   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 491 
 492   if (is_qtype) { // Logically creates elements, ensure klass init
 493     klass->initialize(CHECK);
 494   }
 495 
 496   // We must create an array of jints to pass to multi_allocate.
 497   ResourceMark rm(current);
 498   const int small_dims = 10;
 499   jint dim_array[small_dims];
 500   jint *dims = &dim_array[0];
 501   if (nof_dims > small_dims) {
 502     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 503   }
 504   for (int index = 0; index < nof_dims; index++) {
 505     // offset from first_size_address is addressed as local[index]
 506     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 507     dims[index] = first_size_address[n];
 508   }
 509   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 510   current->set_vm_result(obj);
 511 JRT_END
 512 
 513 
 514 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 515   assert(oopDesc::is_oop(obj), "must be a valid oop");
 516   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 517   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 518 JRT_END
 519 
 520 JRT_ENTRY(jboolean, InterpreterRuntime::is_substitutable(JavaThread* current, oopDesc* aobj, oopDesc* bobj))
 521   assert(oopDesc::is_oop(aobj) && oopDesc::is_oop(bobj), "must be valid oops");
 522 
 523   Handle ha(THREAD, aobj);
 524   Handle hb(THREAD, bobj);
 525   JavaValue result(T_BOOLEAN);
 526   JavaCallArguments args;
 527   args.push_oop(ha);
 528   args.push_oop(hb);
 529   methodHandle method(current, Universe::is_substitutable_method());
 530   JavaCalls::call(&result, method, &args, THREAD);
 531   if (HAS_PENDING_EXCEPTION) {
 532     // Something really bad happened because isSubstitutable() should not throw exceptions
 533     // If it is an error, just let it propagate
 534     // If it is an exception, wrap it into an InternalError
 535     if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 536       Handle e(THREAD, PENDING_EXCEPTION);
 537       CLEAR_PENDING_EXCEPTION;
 538       THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
 539     }
 540   }
 541   return result.get_jboolean();
 542 JRT_END
 543 
 544 // Quicken instance-of and check-cast bytecodes
 545 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 546   // Force resolving; quicken the bytecode
 547   LastFrameAccessor last_frame(current);
 548   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 549   ConstantPool* cpool = last_frame.method()->constants();
 550   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 551   // program we might have seen an unquick'd bytecode in the interpreter but have another
 552   // thread quicken the bytecode before we get here.
 553   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 554   Klass* klass = cpool->klass_at(which, CHECK);
 555   current->set_vm_result_2(klass);
 556 JRT_END
 557 
 558 
 559 //------------------------------------------------------------------------------------------------------------------------
 560 // Exceptions
 561 
 562 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 849 // and therefore we don't have the receiver object at our fingertips. (Though,
 850 // on some platforms the receiver still resides in a register...). Thus,
 851 // we have no choice but print an error message not containing the receiver
 852 // type.
 853 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 854                                                                         Method* missingMethod))
 855   ResourceMark rm(current);
 856   assert(missingMethod != NULL, "sanity");
 857   methodHandle m(current, missingMethod);
 858   LinkResolver::throw_abstract_method_error(m, THREAD);
 859 JRT_END
 860 
 861 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 862                                                                      Klass* recvKlass,
 863                                                                      Method* missingMethod))
 864   ResourceMark rm(current);
 865   methodHandle mh = methodHandle(current, missingMethod);
 866   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 867 JRT_END
 868 
 869 JRT_ENTRY(void, InterpreterRuntime::throw_InstantiationError(JavaThread* current))
 870   THROW(vmSymbols::java_lang_InstantiationError());
 871 JRT_END
 872 
 873 
 874 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 875   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 876 JRT_END
 877 
 878 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 879                                                                               Klass* recvKlass,
 880                                                                               Klass* interfaceKlass))
 881   ResourceMark rm(current);
 882   char buf[1000];
 883   buf[0] = '\0';
 884   jio_snprintf(buf, sizeof(buf),
 885                "Class %s does not implement the requested interface %s",
 886                recvKlass ? recvKlass->external_name() : "NULL",
 887                interfaceKlass ? interfaceKlass->external_name() : "NULL");
 888   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 889 JRT_END
 890 
 891 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 892   THROW(vmSymbols::java_lang_NullPointerException());
 893 JRT_END
 894 
 895 //------------------------------------------------------------------------------------------------------------------------
 896 // Fields
 897 //
 898 
 899 void InterpreterRuntime::resolve_get_put(JavaThread* current, Bytecodes::Code bytecode) {
 900   // resolve field
 901   fieldDescriptor info;
 902   LastFrameAccessor last_frame(current);
 903   constantPoolHandle pool(current, last_frame.method()->constants());
 904   methodHandle m(current, last_frame.method());
 905   bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_nofast_putfield ||
 906                     bytecode == Bytecodes::_putstatic || bytecode == Bytecodes::_withfield);
 907   bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
 908   bool is_inline_type  = bytecode == Bytecodes::_withfield;
 909 
 910   {
 911     JvmtiHideSingleStepping jhss(current);
 912     JavaThread* THREAD = current; // For exception macros.
 913     LinkResolver::resolve_field_access(info, pool, last_frame.get_index_u2_cpcache(bytecode),
 914                                        m, bytecode, CHECK);
 915   } // end JvmtiHideSingleStepping
 916 
 917   // check if link resolution caused cpCache to be updated
 918   ConstantPoolCacheEntry* cp_cache_entry = last_frame.cache_entry();
 919   if (cp_cache_entry->is_resolved(bytecode)) return;
 920 
 921   // compute auxiliary field attributes
 922   TosState state  = as_TosState(info.field_type());
 923 
 924   // Resolution of put instructions on final fields is delayed. That is required so that
 925   // exceptions are thrown at the correct place (when the instruction is actually invoked).
 926   // If we do not resolve an instruction in the current pass, leaving the put_code
 927   // set to zero will cause the next put instruction to the same field to reresolve.
 928 

 933   // initializer method <init>. If resolution were not inhibited, a putfield
 934   // in an initializer method could be resolved in the initializer. Subsequent
 935   // putfield instructions to the same field would then use cached information.
 936   // As a result, those instructions would not pass through the VM. That is,
 937   // checks in resolve_field_access() would not be executed for those instructions
 938   // and the required IllegalAccessError would not be thrown.
 939   //
 940   // Also, we need to delay resolving getstatic and putstatic instructions until the
 941   // class is initialized.  This is required so that access to the static
 942   // field will call the initialization function every time until the class
 943   // is completely initialized ala. in 2.17.5 in JVM Specification.
 944   InstanceKlass* klass = info.field_holder();
 945   bool uninitialized_static = is_static && !klass->is_initialized();
 946   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 947                                       info.has_initialized_final_update();
 948   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 949 
 950   Bytecodes::Code get_code = (Bytecodes::Code)0;
 951   Bytecodes::Code put_code = (Bytecodes::Code)0;
 952   if (!uninitialized_static) {
 953     if (is_static) {
 954       get_code = Bytecodes::_getstatic;
 955     } else {
 956       get_code = Bytecodes::_getfield;
 957     }
 958     if (is_put && is_inline_type) {
 959         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_withfield);
 960     } else if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 961         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 962     }
 963   }
 964 
 965   cp_cache_entry->set_field(
 966     get_code,
 967     put_code,
 968     info.field_holder(),
 969     info.index(),
 970     info.offset(),
 971     state,
 972     info.access_flags().is_final(),
 973     info.access_flags().is_volatile(),
 974     info.is_inlined(),
 975     info.signature()->is_Q_signature() && info.is_inline_type()
 976   );
 977 }
 978 
 979 
 980 //------------------------------------------------------------------------------------------------------------------------
 981 // Synchronization
 982 //
 983 // The interpreter's synchronization code is factored out so that it can
 984 // be shared by method invocation and synchronized blocks.
 985 //%note synchronization_3
 986 
 987 //%note monitor_1
 988 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 989 #ifdef ASSERT
 990   current->last_frame().interpreter_frame_verify_monitor(elem);
 991 #endif
 992   Handle h_obj(current, elem->obj());
 993   assert(Universe::heap()->is_in_or_null(h_obj()),
 994          "must be NULL or an object");
 995   ObjectSynchronizer::enter(h_obj, elem->lock(), current);

1187   {
1188     JvmtiHideSingleStepping jhss(current);
1189     JavaThread* THREAD = current; // For exception macros.
1190     LinkResolver::resolve_invoke(info, Handle(), pool,
1191                                  index, bytecode, CHECK);
1192   } // end JvmtiHideSingleStepping
1193 
1194   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
1195   cp_cache_entry->set_dynamic_call(pool, info);
1196 }
1197 
1198 // This function is the interface to the assembly code. It returns the resolved
1199 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
1200 // This function will check for redefinition!
1201 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
1202   switch (bytecode) {
1203   case Bytecodes::_getstatic:
1204   case Bytecodes::_putstatic:
1205   case Bytecodes::_getfield:
1206   case Bytecodes::_putfield:
1207   case Bytecodes::_withfield:
1208     resolve_get_put(current, bytecode);
1209     break;
1210   case Bytecodes::_invokevirtual:
1211   case Bytecodes::_invokespecial:
1212   case Bytecodes::_invokestatic:
1213   case Bytecodes::_invokeinterface:
1214     resolve_invoke(current, bytecode);
1215     break;
1216   case Bytecodes::_invokehandle:
1217     resolve_invokehandle(current);
1218     break;
1219   case Bytecodes::_invokedynamic:
1220     resolve_invokedynamic(current);
1221     break;
1222   default:
1223     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
1224     break;
1225   }
1226 }
1227 JRT_END

1385   // This function is called by the interpreter when the return poll found a reason
1386   // to call the VM. The reason could be that we are returning into a not yet safe
1387   // to access frame. We handle that below.
1388   // Note that this path does not check for single stepping, because we do not want
1389   // to single step when unwinding frames for an exception being thrown. Instead,
1390   // such single stepping code will use the safepoint table, which will use the
1391   // InterpreterRuntime::at_safepoint callback.
1392   StackWatermarkSet::before_unwind(current);
1393 JRT_END
1394 
1395 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1396                                                       ConstantPoolCacheEntry *cp_entry))
1397 
1398   // check the access_flags for the field in the klass
1399 
1400   InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1401   int index = cp_entry->field_index();
1402   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1403 
1404   bool is_static = (obj == NULL);
1405   bool is_inlined = cp_entry->is_inlined();
1406   HandleMark hm(current);
1407 
1408   Handle h_obj;
1409   if (!is_static) {
1410     // non-static field accessors have an object, but we need a handle
1411     h_obj = Handle(current, obj);
1412   }
1413   InstanceKlass* cp_entry_f1 = InstanceKlass::cast(cp_entry->f1_as_klass());
1414   jfieldID fid = jfieldIDWorkaround::to_jfieldID(cp_entry_f1, cp_entry->f2_as_index(), is_static, is_inlined);
1415   LastFrameAccessor last_frame(current);
1416   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), cp_entry_f1, h_obj, fid);
1417 JRT_END
1418 
1419 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1420                                                             ConstantPoolCacheEntry *cp_entry, jvalue *value))
1421 
1422   Klass* k = cp_entry->f1_as_klass();
1423 
1424   // check the access_flags for the field in the klass
1425   InstanceKlass* ik = InstanceKlass::cast(k);
1426   int index = cp_entry->field_index();
1427   // bail out if field modifications are not watched
1428   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1429 
1430   char sig_type = '\0';
1431 
1432   switch(cp_entry->flag_state()) {
1433     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1434     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1435     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1436     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1437     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1438     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1439     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1440     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1441     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1442     default:  ShouldNotReachHere(); return;
1443   }
1444 
1445   // Both Q-signatures and L-signatures are mapped to atos
1446   if (cp_entry->flag_state() == atos && ik->field_signature(index)->is_Q_signature()) {
1447     sig_type = JVM_SIGNATURE_INLINE_TYPE;
1448   }
1449 
1450   bool is_static = (obj == NULL);
1451   bool is_inlined = cp_entry->is_inlined();
1452 
1453   HandleMark hm(current);
1454   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static, is_inlined);
1455   jvalue fvalue;
1456 #ifdef _LP64
1457   fvalue = *value;
1458 #else
1459   // Long/double values are stored unaligned and also noncontiguously with
1460   // tagged stacks.  We can't just do a simple assignment even in the non-
1461   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1462   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1463   // We assume that the two halves of longs/doubles are stored in interpreter
1464   // stack slots in platform-endian order.
1465   jlong_accessor u;
1466   jint* newval = (jint*)value;
1467   u.words[0] = newval[0];
1468   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1469   fvalue.j = u.long_value;
1470 #endif // _LP64
1471 
1472   Handle h_obj;
1473   if (!is_static) {
1474     // non-static field accessors have an object, but we need a handle
< prev index next >