< prev index next >

src/hotspot/share/interpreter/interpreterRuntime.cpp

Print this page

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/symbolTable.hpp"

  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "compiler/compilationPolicy.hpp"
  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/disassembler.hpp"
  34 #include "gc/shared/barrierSetNMethod.hpp"
  35 #include "gc/shared/collectedHeap.hpp"
  36 #include "interpreter/bytecodeTracer.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "interpreter/interpreterRuntime.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "interpreter/templateTable.hpp"
  41 #include "jvm_io.h"
  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/instanceKlass.inline.hpp"
  49 #include "oops/klass.inline.hpp"
  50 #include "oops/methodData.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.inline.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/symbol.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "prims/methodHandles.hpp"
  57 #include "prims/nativeLookup.hpp"
  58 #include "runtime/atomic.hpp"
  59 #include "runtime/continuation.hpp"
  60 #include "runtime/deoptimization.hpp"
  61 #include "runtime/fieldDescriptor.inline.hpp"
  62 #include "runtime/frame.inline.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/icache.hpp"
  65 #include "runtime/interfaceSupport.inline.hpp"
  66 #include "runtime/java.hpp"
  67 #include "runtime/javaCalls.hpp"
  68 #include "runtime/jfieldIDWorkaround.hpp"
  69 #include "runtime/osThread.hpp"
  70 #include "runtime/sharedRuntime.hpp"
  71 #include "runtime/stackWatermarkSet.hpp"
  72 #include "runtime/stubRoutines.hpp"
  73 #include "runtime/synchronizer.hpp"
  74 #include "runtime/threadCritical.hpp"
  75 #include "utilities/align.hpp"
  76 #include "utilities/copy.hpp"
  77 #include "utilities/events.hpp"

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

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


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

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




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


































































































































































































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







 257   current->set_vm_result(obj);
 258 JRT_END
 259 










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

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




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























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

 611 // and therefore we don't have the receiver object at our fingertips. (Though,
 612 // on some platforms the receiver still resides in a register...). Thus,
 613 // we have no choice but print an error message not containing the receiver
 614 // type.
 615 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 616                                                                         Method* missingMethod))
 617   ResourceMark rm(current);
 618   assert(missingMethod != NULL, "sanity");
 619   methodHandle m(current, missingMethod);
 620   LinkResolver::throw_abstract_method_error(m, THREAD);
 621 JRT_END
 622 
 623 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 624                                                                      Klass* recvKlass,
 625                                                                      Method* missingMethod))
 626   ResourceMark rm(current);
 627   methodHandle mh = methodHandle(current, missingMethod);
 628   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 629 JRT_END
 630 




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

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

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






 713     }
 714   }
 715 
 716   cp_cache_entry->set_field(
 717     get_code,
 718     put_code,
 719     info.field_holder(),
 720     info.index(),
 721     info.offset(),
 722     state,
 723     info.access_flags().is_final(),
 724     info.access_flags().is_volatile()


 725   );
 726 }
 727 
 728 
 729 //------------------------------------------------------------------------------------------------------------------------
 730 // Synchronization
 731 //
 732 // The interpreter's synchronization code is factored out so that it can
 733 // be shared by method invocation and synchronized blocks.
 734 //%note synchronization_3
 735 
 736 //%note monitor_1
 737 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 738 #ifdef ASSERT
 739   current->last_frame().interpreter_frame_verify_monitor(elem);
 740 #endif
 741   Handle h_obj(current, elem->obj());
 742   assert(Universe::heap()->is_in_or_null(h_obj()),
 743          "must be NULL or an object");
 744   ObjectSynchronizer::enter(h_obj, elem->lock(), current);

 943   {
 944     JvmtiHideSingleStepping jhss(current);
 945     JavaThread* THREAD = current; // For exception macros.
 946     LinkResolver::resolve_invoke(info, Handle(), pool,
 947                                  index, bytecode, CHECK);
 948   } // end JvmtiHideSingleStepping
 949 
 950   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
 951   cp_cache_entry->set_dynamic_call(pool, info);
 952 }
 953 
 954 // This function is the interface to the assembly code. It returns the resolved
 955 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
 956 // This function will check for redefinition!
 957 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
 958   switch (bytecode) {
 959   case Bytecodes::_getstatic:
 960   case Bytecodes::_putstatic:
 961   case Bytecodes::_getfield:
 962   case Bytecodes::_putfield:

 963     resolve_get_put(current, bytecode);
 964     break;
 965   case Bytecodes::_invokevirtual:
 966   case Bytecodes::_invokespecial:
 967   case Bytecodes::_invokestatic:
 968   case Bytecodes::_invokeinterface:
 969     resolve_invoke(current, bytecode);
 970     break;
 971   case Bytecodes::_invokehandle:
 972     resolve_invokehandle(current);
 973     break;
 974   case Bytecodes::_invokedynamic:
 975     resolve_invokedynamic(current);
 976     break;
 977   default:
 978     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
 979     break;
 980   }
 981 }
 982 JRT_END

1145   // This function is called by the interpreter when the return poll found a reason
1146   // to call the VM. The reason could be that we are returning into a not yet safe
1147   // to access frame. We handle that below.
1148   // Note that this path does not check for single stepping, because we do not want
1149   // to single step when unwinding frames for an exception being thrown. Instead,
1150   // such single stepping code will use the safepoint table, which will use the
1151   // InterpreterRuntime::at_safepoint callback.
1152   StackWatermarkSet::before_unwind(current);
1153 JRT_END
1154 
1155 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1156                                                       ConstantPoolCacheEntry *cp_entry))
1157 
1158   // check the access_flags for the field in the klass
1159 
1160   InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1161   int index = cp_entry->field_index();
1162   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1163 
1164   bool is_static = (obj == NULL);

1165   HandleMark hm(current);
1166 
1167   Handle h_obj;
1168   if (!is_static) {
1169     // non-static field accessors have an object, but we need a handle
1170     h_obj = Handle(current, obj);
1171   }
1172   InstanceKlass* cp_entry_f1 = InstanceKlass::cast(cp_entry->f1_as_klass());
1173   jfieldID fid = jfieldIDWorkaround::to_jfieldID(cp_entry_f1, cp_entry->f2_as_index(), is_static);
1174   LastFrameAccessor last_frame(current);
1175   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), cp_entry_f1, h_obj, fid);
1176 JRT_END
1177 
1178 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1179                                                             ConstantPoolCacheEntry *cp_entry, jvalue *value))
1180 
1181   Klass* k = cp_entry->f1_as_klass();
1182 
1183   // check the access_flags for the field in the klass
1184   InstanceKlass* ik = InstanceKlass::cast(k);
1185   int index = cp_entry->field_index();
1186   // bail out if field modifications are not watched
1187   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1188 
1189   char sig_type = '\0';
1190 
1191   switch(cp_entry->flag_state()) {
1192     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1193     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1194     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1195     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1196     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1197     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1198     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1199     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1200     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1201     default:  ShouldNotReachHere(); return;
1202   }






1203   bool is_static = (obj == NULL);

1204 
1205   HandleMark hm(current);
1206   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static);
1207   jvalue fvalue;
1208 #ifdef _LP64
1209   fvalue = *value;
1210 #else
1211   // Long/double values are stored unaligned and also noncontiguously with
1212   // tagged stacks.  We can't just do a simple assignment even in the non-
1213   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1214   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1215   // We assume that the two halves of longs/doubles are stored in interpreter
1216   // stack slots in platform-endian order.
1217   jlong_accessor u;
1218   jint* newval = (jint*)value;
1219   u.words[0] = newval[0];
1220   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1221   fvalue.j = u.long_value;
1222 #endif // _LP64
1223 
1224   Handle h_obj;
1225   if (!is_static) {
1226     // non-static field accessors have an object, but we need a handle

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.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/bytecodeTracer.hpp"
  38 #include "interpreter/interpreter.hpp"
  39 #include "interpreter/interpreterRuntime.hpp"
  40 #include "interpreter/linkResolver.hpp"
  41 #include "interpreter/templateTable.hpp"
  42 #include "jvm_io.h"
  43 #include "logging/log.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/constantPool.hpp"
  48 #include "oops/cpCache.inline.hpp"
  49 #include "oops/flatArrayKlass.hpp"
  50 #include "oops/flatArrayOop.inline.hpp"
  51 #include "oops/inlineKlass.inline.hpp"
  52 #include "oops/instanceKlass.inline.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/methodData.hpp"
  55 #include "oops/objArrayKlass.hpp"
  56 #include "oops/objArrayOop.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "oops/symbol.hpp"
  59 #include "prims/jvmtiExport.hpp"
  60 #include "prims/methodHandles.hpp"
  61 #include "prims/nativeLookup.hpp"
  62 #include "runtime/atomic.hpp"
  63 #include "runtime/continuation.hpp"
  64 #include "runtime/deoptimization.hpp"
  65 #include "runtime/fieldDescriptor.inline.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/icache.hpp"
  69 #include "runtime/interfaceSupport.inline.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/jfieldIDWorkaround.hpp"
  73 #include "runtime/osThread.hpp"
  74 #include "runtime/sharedRuntime.hpp"
  75 #include "runtime/stackWatermarkSet.hpp"
  76 #include "runtime/stubRoutines.hpp"
  77 #include "runtime/synchronizer.hpp"
  78 #include "runtime/threadCritical.hpp"
  79 #include "utilities/align.hpp"
  80 #include "utilities/copy.hpp"
  81 #include "utilities/events.hpp"
  82 #include "utilities/globalDefinitions.hpp"
  83 #ifdef COMPILER2
  84 #include "opto/runtime.hpp"
  85 #endif
  86 
  87 // Helper class to access current interpreter state
  88 class LastFrameAccessor : public StackObj {
  89   frame _last_frame;
  90 public:
  91   LastFrameAccessor(JavaThread* current) {
  92     assert(current == Thread::current(), "sanity");
  93     _last_frame = current->last_frame();
  94   }
  95   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  96   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  97   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  98   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  99   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
 100 
 101   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 102   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

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

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

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

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

1206   {
1207     JvmtiHideSingleStepping jhss(current);
1208     JavaThread* THREAD = current; // For exception macros.
1209     LinkResolver::resolve_invoke(info, Handle(), pool,
1210                                  index, bytecode, CHECK);
1211   } // end JvmtiHideSingleStepping
1212 
1213   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
1214   cp_cache_entry->set_dynamic_call(pool, info);
1215 }
1216 
1217 // This function is the interface to the assembly code. It returns the resolved
1218 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
1219 // This function will check for redefinition!
1220 JRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* current, Bytecodes::Code bytecode)) {
1221   switch (bytecode) {
1222   case Bytecodes::_getstatic:
1223   case Bytecodes::_putstatic:
1224   case Bytecodes::_getfield:
1225   case Bytecodes::_putfield:
1226   case Bytecodes::_withfield:
1227     resolve_get_put(current, bytecode);
1228     break;
1229   case Bytecodes::_invokevirtual:
1230   case Bytecodes::_invokespecial:
1231   case Bytecodes::_invokestatic:
1232   case Bytecodes::_invokeinterface:
1233     resolve_invoke(current, bytecode);
1234     break;
1235   case Bytecodes::_invokehandle:
1236     resolve_invokehandle(current);
1237     break;
1238   case Bytecodes::_invokedynamic:
1239     resolve_invokedynamic(current);
1240     break;
1241   default:
1242     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
1243     break;
1244   }
1245 }
1246 JRT_END

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