< 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/bytecodeTracer.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/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,

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




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

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

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






 707     }
 708   }
 709 
 710   cp_cache_entry->set_field(
 711     get_code,
 712     put_code,
 713     info.field_holder(),
 714     info.index(),
 715     info.offset(),
 716     state,
 717     info.access_flags().is_final(),
 718     info.access_flags().is_volatile()


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

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

 957     resolve_get_put(current, bytecode);
 958     break;
 959   case Bytecodes::_invokevirtual:
 960   case Bytecodes::_invokespecial:
 961   case Bytecodes::_invokestatic:
 962   case Bytecodes::_invokeinterface:
 963     resolve_invoke(current, bytecode);
 964     break;
 965   case Bytecodes::_invokehandle:
 966     resolve_invokehandle(current);
 967     break;
 968   case Bytecodes::_invokedynamic:
 969     resolve_invokedynamic(current);
 970     break;
 971   default:
 972     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
 973     break;
 974   }
 975 }
 976 JRT_END

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

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






1196   bool is_static = (obj == NULL);

1197 
1198   HandleMark hm(current);
1199   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static);
1200   jvalue fvalue;
1201 #ifdef _LP64
1202   fvalue = *value;
1203 #else
1204   // Long/double values are stored unaligned and also noncontiguously with
1205   // tagged stacks.  We can't just do a simple assignment even in the non-
1206   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1207   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1208   // We assume that the two halves of longs/doubles are stored in interpreter
1209   // stack slots in platform-endian order.
1210   jlong_accessor u;
1211   jint* newval = (jint*)value;
1212   u.words[0] = newval[0];
1213   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1214   fvalue.j = u.long_value;
1215 #endif // _LP64
1216 
1217   Handle h_obj;
1218   if (!is_static) {
1219     // 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/bytecodeTracer.hpp"
  39 #include "interpreter/interpreter.hpp"
  40 #include "interpreter/interpreterRuntime.hpp"
  41 #include "interpreter/linkResolver.hpp"
  42 #include "interpreter/templateTable.hpp"
  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,

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

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

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

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