1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "compiler/disassembler.hpp"
  32 #include "gc/shared/collectedHeap.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interpreterRuntime.hpp"
  35 #include "interpreter/linkResolver.hpp"
  36 #include "interpreter/templateTable.hpp"
  37 #include "logging/log.hpp"
  38 #include "memory/oopFactory.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "memory/universe.inline.hpp"
  41 #include "memory/vtBuffer.hpp"
  42 #include "oops/constantPool.hpp"
  43 #include "oops/instanceKlass.hpp"
  44 #include "oops/methodData.hpp"
  45 #include "oops/objArrayKlass.hpp"
  46 #include "oops/objArrayOop.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "oops/symbol.hpp"
  49 #include "oops/valueKlass.hpp"
  50 #include "oops/valueArrayKlass.hpp"
  51 #include "oops/valueArrayOop.hpp"
  52 #include "prims/jvmtiExport.hpp"
  53 #include "prims/nativeLookup.hpp"
  54 #include "runtime/atomic.hpp"
  55 #include "runtime/biasedLocking.hpp"
  56 #include "runtime/compilationPolicy.hpp"
  57 #include "runtime/deoptimization.hpp"
  58 #include "runtime/fieldDescriptor.hpp"
  59 #include "runtime/handles.inline.hpp"
  60 #include "runtime/icache.hpp"
  61 #include "runtime/interfaceSupport.hpp"
  62 #include "runtime/java.hpp"
  63 #include "runtime/jfieldIDWorkaround.hpp"
  64 #include "runtime/osThread.hpp"
  65 #include "runtime/sharedRuntime.hpp"
  66 #include "runtime/stubRoutines.hpp"
  67 #include "runtime/synchronizer.hpp"
  68 #include "runtime/threadCritical.hpp"
  69 #include "utilities/align.hpp"
  70 #include "utilities/events.hpp"
  71 #include "utilities/globalDefinitions.hpp"
  72 #ifdef COMPILER2
  73 #include "opto/runtime.hpp"
  74 #endif
  75 
  76 class UnlockFlagSaver {
  77   private:
  78     JavaThread* _thread;
  79     bool _do_not_unlock;
  80   public:
  81     UnlockFlagSaver(JavaThread* t) {
  82       _thread = t;
  83       _do_not_unlock = t->do_not_unlock_if_synchronized();
  84       t->set_do_not_unlock_if_synchronized(false);
  85     }
  86     ~UnlockFlagSaver() {
  87       _thread->set_do_not_unlock_if_synchronized(_do_not_unlock);
  88     }
  89 };
  90 
  91 //------------------------------------------------------------------------------------------------------------------------
  92 // State accessors
  93 
  94 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread *thread) {
  95   last_frame(thread).interpreter_frame_set_bcp(bcp);
  96   if (ProfileInterpreter) {
  97     // ProfileTraps uses MDOs independently of ProfileInterpreter.
  98     // That is why we must check both ProfileInterpreter and mdo != NULL.
  99     MethodData* mdo = last_frame(thread).interpreter_frame_method()->method_data();
 100     if (mdo != NULL) {
 101       NEEDS_CLEANUP;
 102       last_frame(thread).interpreter_frame_set_mdp(mdo->bci_to_dp(last_frame(thread).interpreter_frame_bci()));
 103     }
 104   }
 105 }
 106 
 107 //------------------------------------------------------------------------------------------------------------------------
 108 // Constants
 109 
 110 
 111 IRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* thread, bool wide))
 112   // access constant pool
 113   ConstantPool* pool = method(thread)->constants();
 114   int index = wide ? get_index_u2(thread, Bytecodes::_ldc_w) : get_index_u1(thread, Bytecodes::_ldc);
 115   constantTag tag = pool->tag_at(index);
 116 
 117   assert ((tag.is_unresolved_klass() || tag.is_klass() ||
 118            tag.is_unresolved_value_type() || tag.is_value_type()),
 119           "wrong ldc call");
 120   Klass* klass = pool->klass_at(index, CHECK);
 121     oop java_class = klass->java_mirror();
 122     thread->set_vm_result(java_class);
 123 IRT_END
 124 
 125 IRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* thread, Bytecodes::Code bytecode)) {
 126   assert(bytecode == Bytecodes::_fast_aldc ||
 127          bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
 128   ResourceMark rm(thread);
 129   methodHandle m (thread, method(thread));
 130   Bytecode_loadconstant ldc(m, bci(thread));
 131   oop result = ldc.resolve_constant(CHECK);
 132 #ifdef ASSERT
 133   {
 134     // The bytecode wrappers aren't GC-safe so construct a new one
 135     Bytecode_loadconstant ldc2(m, bci(thread));
 136     oop coop = m->constants()->resolved_references()->obj_at(ldc2.cache_index());
 137     assert(result == coop, "expected result for assembly code");
 138   }
 139 #endif
 140   thread->set_vm_result(result);
 141 }
 142 IRT_END
 143 
 144 
 145 //------------------------------------------------------------------------------------------------------------------------
 146 // Allocation
 147 
 148 IRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* thread, ConstantPool* pool, int index))
 149   Klass* k = pool->klass_at(index, CHECK);
 150   InstanceKlass* klass = InstanceKlass::cast(k);
 151 
 152   // Make sure we are not instantiating an abstract klass
 153   klass->check_valid_for_instantiation(true, CHECK);
 154 
 155   // Make sure klass is initialized
 156   klass->initialize(CHECK);
 157 
 158   // At this point the class may not be fully initialized
 159   // because of recursive initialization. If it is fully
 160   // initialized & has_finalized is not set, we rewrite
 161   // it into its fast version (Note: no locking is needed
 162   // here since this is an atomic byte write and can be
 163   // done more than once).
 164   //
 165   // Note: In case of classes with has_finalized we don't
 166   //       rewrite since that saves us an extra check in
 167   //       the fast version which then would call the
 168   //       slow version anyway (and do a call back into
 169   //       Java).
 170   //       If we have a breakpoint, then we don't rewrite
 171   //       because the _breakpoint bytecode would be lost.
 172   oop obj = klass->allocate_instance(CHECK);
 173   thread->set_vm_result(obj);
 174 IRT_END
 175 
 176 void copy_primitive_argument(intptr_t* addr, Handle instance, int offset, BasicType type) {
 177   switch (type) {
 178   case T_BOOLEAN:
 179     instance()->bool_field_put(offset, (jboolean)*((int*)addr));
 180     break;
 181   case T_CHAR:
 182     instance()->char_field_put(offset, (jchar) *((int*)addr));
 183     break;
 184   case T_FLOAT:
 185     instance()->float_field_put(offset, (jfloat)*((float*)addr));
 186     break;
 187   case T_DOUBLE:
 188     instance()->double_field_put(offset, (jdouble)*((double*)addr));
 189     break;
 190   case T_BYTE:
 191     instance()->byte_field_put(offset, (jbyte)*((int*)addr));
 192     break;
 193   case T_SHORT:
 194     instance()->short_field_put(offset, (jshort)*((int*)addr));
 195     break;
 196   case T_INT:
 197     instance()->int_field_put(offset, (jint)*((int*)addr));
 198     break;
 199   case T_LONG:
 200     instance()->long_field_put(offset, (jlong)*((long long*)addr));
 201     break;
 202   case T_OBJECT:
 203   case T_ARRAY:
 204   case T_VALUETYPE:
 205     fatal("Should not be handled with this method");
 206     break;
 207   default:
 208     fatal("Unsupported BasicType");
 209   }
 210 }
 211 
 212 IRT_ENTRY(void, InterpreterRuntime::vdefault(JavaThread* thread, ConstantPool* pool, int index))
 213   // Getting the ValueKlass
 214   Klass* k = pool->klass_at(index, CHECK);
 215   assert(k->is_value(), "vdefault argument must be the value type class");
 216   ValueKlass* vklass = ValueKlass::cast(k);
 217 
 218   vklass->initialize(THREAD);
 219 
 220   // Creating value
 221   bool in_heap;
 222   instanceOop value = vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK);
 223   Handle value_h = Handle(THREAD, value);
 224 
 225   thread->set_vm_result(value_h());
 226 IRT_END
 227 
 228 IRT_ENTRY(int, InterpreterRuntime::vwithfield(JavaThread* thread, ConstantPoolCache* cp_cache))
 229   // Getting the ValueKlass
 230   int index = ConstantPool::decode_cpcache_index(get_index_u2_cpcache(thread, Bytecodes::_vwithfield));
 231   ConstantPoolCacheEntry* cp_entry = cp_cache->entry_at(index);
 232   assert(cp_entry->is_resolved(Bytecodes::_vwithfield), "Should have been resolved");
 233   Klass* klass = cp_entry->f1_as_klass();
 234   assert(klass->is_value(), "vwithfield only applies to value types");
 235   ValueKlass* vklass = ValueKlass::cast(klass);
 236 
 237   // Getting Field information
 238   int offset = cp_entry->f2_as_index();
 239   int field_index = cp_entry->field_index();
 240   int field_offset = cp_entry->f2_as_offset();
 241   Symbol* field_signature = vklass->field_signature(field_index);
 242   ResourceMark rm(THREAD);
 243   const char* signature = (const char *) field_signature->as_utf8();
 244   BasicType field_type = char2type(signature[0]);
 245 
 246   // Getting old value
 247   frame f = last_frame(thread);
 248   jint tos_idx = f.interpreter_frame_expression_stack_size() - 1;
 249   int vt_offset = type2size[field_type];
 250   oop old_value = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx - vt_offset);
 251   assert(old_value != NULL && oopDesc::is_oop(old_value) && old_value->is_value(),"Verifying receiver");
 252   Handle old_value_h(THREAD, old_value);
 253 
 254   // Creating new value by copying the one passed in argument
 255   bool in_heap;
 256   instanceOop new_value = vklass->allocate_buffered_or_heap_instance(&in_heap,
 257       CHECK_((type2size[field_type]) * AbstractInterpreter::stackElementSize));
 258   Handle new_value_h = Handle(THREAD, new_value);
 259   int first_offset = vklass->first_field_offset();
 260   vklass->value_store(vklass->data_for_oop(old_value_h()),
 261       vklass->data_for_oop(new_value_h()), in_heap, false);
 262 
 263   // Updating the field specified in arguments
 264   if (field_type == T_OBJECT || field_type == T_ARRAY) {
 265     oop aoop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx);
 266     assert(aoop == NULL || (oopDesc::is_oop(aoop) && (!aoop->is_value())),"argument must be a reference type");
 267     new_value_h()->obj_field_put(field_offset, aoop);
 268   } else if (field_type == T_VALUETYPE) {
 269     if (cp_entry->is_flatten()) {
 270       Klass* field_k = vklass->get_value_field_klass(field_index);
 271       ValueKlass* field_vk = ValueKlass::cast(field_k);
 272       oop vt_oop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx);
 273       assert(vt_oop != NULL && oopDesc::is_oop(vt_oop) && vt_oop->is_value(),"argument must be a value type");
 274       assert(field_vk == vt_oop->klass(), "Must match");
 275       field_vk->value_store(field_vk->data_for_oop(vt_oop),
 276           ((char*)(oopDesc*)new_value_h()) + field_offset, true, false);
 277     } else {
 278       oop voop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx);
 279       assert(voop != NULL || (oopDesc::is_oop(voop) && (voop->is_value())),"argument must be a value type");
 280       new_value_h()->obj_field_put(field_offset, voop);
 281     }
 282   } else {
 283     intptr_t* addr = f.interpreter_frame_expression_stack_at(tos_idx);
 284     copy_primitive_argument(addr, new_value_h, field_offset, field_type);
 285   }
 286 
 287   // returning result
 288   thread->set_vm_result(new_value_h());
 289   return (type2size[field_type] + type2size[T_VALUETYPE]) * AbstractInterpreter::stackElementSize;
 290 IRT_END
 291 
 292 IRT_ENTRY(void, InterpreterRuntime::vbox(JavaThread* thread, ConstantPool* pool, int index, oopDesc* value))
 293   assert(EnableMVT, "vbox is supported only when the MVT programming model is enabled");
 294   if (value == NULL) {
 295     THROW(vmSymbols::java_lang_NullPointerException());
 296   }
 297 
 298   // Since the verifier is probably disabled, a few extra type check
 299   Klass* target_klass = pool->klass_at(index, CHECK);
 300   if (target_klass->is_value()) {
 301     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox target is value type");
 302   }
 303   Klass* klass = value->klass();
 304   if (!klass->is_value()) {
 305     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox not from value type");
 306   }
 307   ValueKlass* vtklass = ValueKlass::cast(klass);
 308   if (vtklass->get_vcc_klass() != target_klass) {
 309     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox target is not derive value type box");
 310   }
 311   oop box = vtklass->box(Handle(THREAD, value),
 312                          InstanceKlass::cast(target_klass),
 313                          CHECK);
 314   thread->set_vm_result(box);
 315 IRT_END
 316 
 317 IRT_ENTRY(void, InterpreterRuntime::vunbox(JavaThread* thread, ConstantPool* pool, int index, oopDesc* obj))
 318 assert(EnableMVT, "vunbox is supported only when the MVT programming model is enabled");
 319   if (obj == NULL) {
 320     THROW(vmSymbols::java_lang_NullPointerException());
 321   }
 322   Klass* target_klass = pool->klass_at(index, CHECK);
 323   if (!target_klass->is_value()) {
 324     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox target is not value type");
 325   }
 326   Klass* klass = obj->klass();
 327   if ((!klass->is_instance_klass()) || klass->is_value()) {
 328     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox source is not an instance");
 329   }
 330     if (klass != InstanceKlass::cast(target_klass)->get_vcc_klass()) {
 331     THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox target is not derive value type");
 332   }
 333   oop value = ValueKlass::cast(target_klass)->unbox(Handle(THREAD, obj),
 334                                                 InstanceKlass::cast(target_klass),
 335                                                 CHECK);
 336   thread->set_vm_result(value);
 337 IRT_END
 338 
 339 IRT_ENTRY(void, InterpreterRuntime::qgetfield(JavaThread* thread, oopDesc* obj, int index))
 340   Handle value_h(THREAD, obj);
 341   InstanceKlass* klass = InstanceKlass::cast(obj->klass());
 342 
 343   Klass* field_k = klass->get_value_field_klass(index);
 344   ValueKlass* field_vklass = ValueKlass::cast(field_k);
 345   field_vklass->initialize(THREAD);
 346 
 347   instanceOop res;
 348   bool in_heap;
 349   if (klass->is_field_flatten(index)) {
 350     // allocate instance
 351     res = field_vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK);
 352     instanceHandle res_h(THREAD, res);
 353     // copy value
 354     field_vklass->value_store(((char*)(oopDesc*)value_h()) + klass->field_offset(index),
 355                               field_vklass->data_for_oop(res), in_heap, false);
 356     thread->set_vm_result(res_h());
 357   } else {
 358     oop res = value_h()->obj_field_acquire(klass->field_offset(index));
 359     if (res == NULL) {
 360       res = field_vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK);
 361     }
 362     thread->set_vm_result(res);
 363   }
 364 IRT_END
 365 
 366 IRT_ENTRY(void, InterpreterRuntime::initialize_static_value_field(JavaThread* thread, oopDesc* mirror, int index))
 367   instanceHandle mirror_h(THREAD, (instanceOop)mirror);
 368   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
 369   int offset = klass->field_offset(index);
 370   assert(mirror->obj_field(offset) == NULL,"Field must not be initialized twice");
 371 
 372   Klass* field_k = klass->get_value_field_klass(index);
 373   ValueKlass* field_vklass = ValueKlass::cast(field_k);
 374   // allocate instance, because it is going to be assigned to a static field
 375   // it must not be a buffered value
 376   instanceOop res = field_vklass->allocate_instance(CHECK);
 377   instanceHandle res_h(THREAD, res);
 378   mirror_h()->obj_field_put(offset, res_h());
 379   thread->set_vm_result(res_h());
 380 IRT_END
 381 
 382 IRT_ENTRY(void, InterpreterRuntime::qputfield(JavaThread* thread, oopDesc* obj, oopDesc* value, int flags))
 383   Handle value_h(THREAD, value);
 384   Handle obj_h(THREAD, obj);
 385   assert(!obj_h()->klass()->is_value(), "obj must be an object");
 386   assert(value_h()->klass()->is_value(), "value must be an value type");
 387   int index = flags & ConstantPoolCacheEntry::field_index_mask;
 388   bool flatten = (flags & (1 << ConstantPoolCacheEntry::is_flatten_field)) != 0;
 389 
 390   InstanceKlass* klass = InstanceKlass::cast(obj->klass());
 391   Klass* field_k = klass->get_value_field_klass(index);
 392   ValueKlass* field_vklass = ValueKlass::cast(value->klass());
 393   assert(field_k == field_vklass, "Field descriptor and argument must match");
 394   if (flatten) {
 395     // copy value
 396     field_vklass->value_store(field_vklass->data_for_oop(value_h()),
 397                               ((char*)(oopDesc*)obj_h()) + klass->field_offset(index), true, false);
 398   } else {
 399     if (Universe::heap()->is_in_reserved(value_h())) {
 400       obj_h()->obj_field_put(klass->field_offset(index), value_h());
 401     } else {
 402       // allocate heap instance
 403       instanceOop val = field_vklass->allocate_instance(CHECK);
 404       instanceHandle res_h(THREAD, val);
 405       // copy value
 406       field_vklass->value_store(field_vklass->data_for_oop(value_h()),
 407                                 field_vklass->data_for_oop(res_h()), true, false);
 408 
 409 
 410       obj_h()->obj_field_put(klass->field_offset(index), res_h());
 411     }
 412   }
 413 IRT_END
 414 
 415 IRT_ENTRY(void, InterpreterRuntime::qputstatic(JavaThread* thread, oopDesc* value, int offset, oopDesc* mirror))
 416   instanceHandle value_h(THREAD, (instanceOop)value);
 417   assert(value_h()->is_value(), "qputstatic only deals with value arguments");
 418   if (Universe::heap()->is_in_reserved(value_h())) {
 419       mirror->obj_field_put(offset, value_h());
 420   } else {
 421     // The argument is a buffered value, a copy must be created in the Java heap
 422     // because a static field cannot point to a thread-local buffered value
 423     ValueKlass* field_vklass = ValueKlass::cast(value_h()->klass());
 424     Handle mirror_h(THREAD, mirror);
 425     // allocate heap instance
 426     instanceOop res = field_vklass->allocate_instance(CHECK);
 427     assert(Universe::heap()->is_in_reserved(res), "Must be in the Java heap");
 428     instanceHandle res_h(THREAD, res);
 429     // copy value
 430     field_vklass->value_store(field_vklass->data_for_oop(value_h()),
 431                               field_vklass->data_for_oop(res), true, false);
 432     // writing static field
 433     mirror_h->obj_field_put(offset, res_h());
 434     assert(mirror_h->obj_field(offset) != NULL,"Sanity check");
 435   }
 436 IRT_END
 437 
 438 IRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* thread, BasicType type, jint size))
 439   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 440   thread->set_vm_result(obj);
 441 IRT_END
 442 
 443 
 444 IRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* thread, ConstantPool* pool, int index, jint size))
 445   Klass*    klass = pool->klass_at(index, CHECK);
 446   if (klass->is_value()) { // Logically creates elements, ensure klass init
 447     klass->initialize(CHECK);
 448   }
 449   arrayOop obj = oopFactory::new_array(klass, size, CHECK);
 450   thread->set_vm_result(obj);
 451 IRT_END
 452 
 453 IRT_ENTRY(void, InterpreterRuntime::value_array_load(JavaThread* thread, arrayOopDesc* array, int index))
 454   Klass* klass = array->klass();
 455   assert(klass->is_valueArray_klass() || klass->is_objArray_klass(), "expected value or object array oop");
 456 
 457   if (klass->is_objArray_klass()) {
 458     thread->set_vm_result(((objArrayOop) array)->obj_at(index));
 459   }
 460   else {
 461     ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
 462     ValueKlass* vklass = vaklass->element_klass();
 463     arrayHandle ah(THREAD, array);
 464     bool in_heap;
 465     instanceOop value_holder = vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK);
 466     void* src = ((valueArrayOop)ah())->value_at_addr(index, vaklass->layout_helper());
 467     vklass->value_store(src, vklass->data_for_oop(value_holder),
 468                           vaklass->element_byte_size(), in_heap, false);
 469     thread->set_vm_result(value_holder);
 470   }
 471 IRT_END
 472 
 473 IRT_ENTRY(void, InterpreterRuntime::value_array_store(JavaThread* thread, arrayOopDesc* array, int index, void* val))
 474   Klass* klass = array->klass();
 475   assert(klass->is_valueArray_klass() || klass->is_objArray_klass(), "expected value or object array oop");
 476 
 477   if (ArrayKlass::cast(klass)->element_klass() != ((oop)val)->klass()) {
 478     THROW(vmSymbols::java_lang_ArrayStoreException());
 479   }
 480   if (klass->is_objArray_klass()) {
 481     if(!Universe::heap()->is_in_reserved(val)) {
 482       // A Java heap allocated copy must be made because an array cannot
 483       // reference a thread-local buffered value
 484       Handle val_h(THREAD, (oop)val);
 485       ObjArrayKlass* aklass = ObjArrayKlass::cast(klass);
 486       Klass* eklass = aklass->element_klass();
 487       assert(eklass->is_value(), "Sanity check");
 488       assert(eklass == ((oop)val)->klass(), "Sanity check");
 489       ValueKlass* vklass = ValueKlass::cast(eklass);
 490       // allocate heap instance
 491       instanceOop res = vklass->allocate_instance(CHECK);
 492       Handle res_h(THREAD, res);
 493       // copy value
 494       vklass->value_store(((char*)(oopDesc*)val_h()) + vklass->first_field_offset(),
 495                             ((char*)(oopDesc*)res_h()) + vklass->first_field_offset(),true, false);
 496       val = res_h();
 497     }
 498     ((objArrayOop) array)->obj_at_put(index, (oop)val);
 499   } else {
 500     valueArrayOop varray = (valueArrayOop)array;
 501     ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
 502     ValueKlass* vklass = vaklass->element_klass();
 503     const int lh = vaklass->layout_helper();
 504     vklass->value_store(vklass->data_for_oop((oop)val), varray->value_at_addr(index, lh),
 505                         vaklass->element_byte_size(), true, false);
 506   }
 507 IRT_END
 508 
 509 IRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* thread, jint* first_size_address))
 510   // We may want to pass in more arguments - could make this slightly faster
 511   ConstantPool* constants = method(thread)->constants();
 512   int          i = get_index_u2(thread, Bytecodes::_multianewarray);
 513   Klass* klass = constants->klass_at(i, CHECK);
 514   int   nof_dims = number_of_dimensions(thread);
 515   assert(klass->is_klass(), "not a class");
 516   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 517 
 518   if (klass->is_value()) { // Logically creates elements, ensure klass init
 519     klass->initialize(CHECK);
 520   }
 521 
 522   // We must create an array of jints to pass to multi_allocate.
 523   ResourceMark rm(thread);
 524   const int small_dims = 10;
 525   jint dim_array[small_dims];
 526   jint *dims = &dim_array[0];
 527   if (nof_dims > small_dims) {
 528     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 529   }
 530   for (int index = 0; index < nof_dims; index++) {
 531     // offset from first_size_address is addressed as local[index]
 532     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 533     dims[index] = first_size_address[n];
 534   }
 535   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 536   thread->set_vm_result(obj);
 537 IRT_END
 538 
 539 IRT_ENTRY(void, InterpreterRuntime::recycle_vtbuffer(JavaThread* thread))
 540   VTBuffer::recycle_vtbuffer(thread, last_frame(thread));
 541 IRT_END
 542 
 543 IRT_ENTRY(void, InterpreterRuntime::recycle_buffered_values(JavaThread* thread))
 544   frame f = thread->last_frame();
 545   assert(f.is_interpreted_frame(), "recycling can only be triggered from interpreted frames");
 546   VTBuffer::recycle_vt_in_frame(thread, &f);
 547 IRT_END
 548 
 549 IRT_ENTRY(void, InterpreterRuntime::fix_frame_vt_alloc_ptr(JavaThread* thread))
 550   frame f = thread->last_frame();
 551   VTBuffer::fix_frame_vt_alloc_ptr(f, VTBufferChunk::chunk(thread->vt_alloc_ptr()));
 552 IRT_END
 553 
 554 IRT_ENTRY(void, InterpreterRuntime::return_value(JavaThread* thread, oopDesc* obj))
 555   if (Universe::heap()->is_in_reserved(obj)) {
 556     thread->set_vm_result(obj);
 557     return;
 558   }
 559   assert(obj->klass()->is_value(), "Sanity check");
 560   ValueKlass* vk = ValueKlass::cast(obj->klass());
 561   RegisterMap reg_map(thread, false);
 562   frame current_frame = last_frame(thread);
 563   frame caller_frame = current_frame.sender(&reg_map);
 564   if (!caller_frame.is_interpreted_frame()) {
 565     // caller is not an interpreted frame, creating a new value in Java heap
 566     Handle obj_h(THREAD, obj);
 567     instanceOop res = vk->allocate_instance(CHECK);
 568     Handle res_h(THREAD, res);
 569     // copy value
 570     vk->value_store(vk->data_for_oop(obj_h()),
 571                     vk->data_for_oop(res_h()), true, false);
 572     thread->set_vm_result(res_h());
 573     return;
 574   } else {
 575     oop dest = VTBuffer::relocate_return_value(thread, current_frame, obj);
 576     thread->set_vm_result(dest);
 577   }
 578 IRT_END
 579 
 580 IRT_ENTRY(void, InterpreterRuntime::check_areturn(JavaThread* thread, oopDesc* obj))
 581   if (obj != NULL) {
 582     Klass* k = obj->klass();
 583     if (k->is_value()) {
 584       ResourceMark rm(thread);
 585       tty->print_cr("areturn used on a value from %s", k->name()->as_C_string());
 586     }
 587     assert(!k->is_value(), "areturn should never be used on values");
 588   }
 589   thread->set_vm_result(obj);
 590 IRT_END
 591 
 592 IRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* thread, oopDesc* obj))
 593   assert(oopDesc::is_oop(obj), "must be a valid oop");
 594   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 595   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 596 IRT_END
 597 
 598 
 599 // Quicken instance-of and check-cast bytecodes
 600 IRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* thread))
 601   // Force resolving; quicken the bytecode
 602   int which = get_index_u2(thread, Bytecodes::_checkcast);
 603   ConstantPool* cpool = method(thread)->constants();
 604   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 605   // program we might have seen an unquick'd bytecode in the interpreter but have another
 606   // thread quicken the bytecode before we get here.
 607   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 608   Klass* klass = cpool->klass_at(which, CHECK);
 609   thread->set_vm_result_2(klass);
 610 IRT_END
 611 
 612 
 613 //------------------------------------------------------------------------------------------------------------------------
 614 // Exceptions
 615 
 616 void InterpreterRuntime::note_trap_inner(JavaThread* thread, int reason,
 617                                          const methodHandle& trap_method, int trap_bci, TRAPS) {
 618   if (trap_method.not_null()) {
 619     MethodData* trap_mdo = trap_method->method_data();
 620     if (trap_mdo == NULL) {
 621       Method::build_interpreter_method_data(trap_method, THREAD);
 622       if (HAS_PENDING_EXCEPTION) {
 623         assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())),
 624                "we expect only an OOM error here");
 625         CLEAR_PENDING_EXCEPTION;
 626       }
 627       trap_mdo = trap_method->method_data();
 628       // and fall through...
 629     }
 630     if (trap_mdo != NULL) {
 631       // Update per-method count of trap events.  The interpreter
 632       // is updating the MDO to simulate the effect of compiler traps.
 633       Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
 634     }
 635   }
 636 }
 637 
 638 // Assume the compiler is (or will be) interested in this event.
 639 // If necessary, create an MDO to hold the information, and record it.
 640 void InterpreterRuntime::note_trap(JavaThread* thread, int reason, TRAPS) {
 641   assert(ProfileTraps, "call me only if profiling");
 642   methodHandle trap_method(thread, method(thread));
 643   int trap_bci = trap_method->bci_from(bcp(thread));
 644   note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
 645 }
 646 
 647 #ifdef CC_INTERP
 648 // As legacy note_trap, but we have more arguments.
 649 IRT_ENTRY(void, InterpreterRuntime::note_trap(JavaThread* thread, int reason, Method *method, int trap_bci))
 650   methodHandle trap_method(method);
 651   note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
 652 IRT_END
 653 
 654 // Class Deoptimization is not visible in BytecodeInterpreter, so we need a wrapper
 655 // for each exception.
 656 void InterpreterRuntime::note_nullCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 657   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_null_check, method, trap_bci); }
 658 void InterpreterRuntime::note_div0Check_trap(JavaThread* thread, Method *method, int trap_bci)
 659   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_div0_check, method, trap_bci); }
 660 void InterpreterRuntime::note_rangeCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 661   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_range_check, method, trap_bci); }
 662 void InterpreterRuntime::note_classCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 663   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_class_check, method, trap_bci); }
 664 void InterpreterRuntime::note_arrayCheck_trap(JavaThread* thread, Method *method, int trap_bci)
 665   { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_array_check, method, trap_bci); }
 666 #endif // CC_INTERP
 667 
 668 
 669 static Handle get_preinitialized_exception(Klass* k, TRAPS) {
 670   // get klass
 671   InstanceKlass* klass = InstanceKlass::cast(k);
 672   assert(klass->is_initialized(),
 673          "this klass should have been initialized during VM initialization");
 674   // create instance - do not call constructor since we may have no
 675   // (java) stack space left (should assert constructor is empty)
 676   Handle exception;
 677   oop exception_oop = klass->allocate_instance(CHECK_(exception));
 678   exception = Handle(THREAD, exception_oop);
 679   if (StackTraceInThrowable) {
 680     java_lang_Throwable::fill_in_stack_trace(exception);
 681   }
 682   return exception;
 683 }
 684 
 685 // Special handling for stack overflow: since we don't have any (java) stack
 686 // space left we use the pre-allocated & pre-initialized StackOverflowError
 687 // klass to create an stack overflow error instance.  We do not call its
 688 // constructor for the same reason (it is empty, anyway).
 689 IRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* thread))
 690   Handle exception = get_preinitialized_exception(
 691                                  SystemDictionary::StackOverflowError_klass(),
 692                                  CHECK);
 693   // Increment counter for hs_err file reporting
 694   Atomic::inc(&Exceptions::_stack_overflow_errors);
 695   THROW_HANDLE(exception);
 696 IRT_END
 697 
 698 IRT_ENTRY(void, InterpreterRuntime::throw_delayed_StackOverflowError(JavaThread* thread))
 699   Handle exception = get_preinitialized_exception(
 700                                  SystemDictionary::StackOverflowError_klass(),
 701                                  CHECK);
 702   java_lang_Throwable::set_message(exception(),
 703           Universe::delayed_stack_overflow_error_message());
 704   // Increment counter for hs_err file reporting
 705   Atomic::inc(&Exceptions::_stack_overflow_errors);
 706   THROW_HANDLE(exception);
 707 IRT_END
 708 
 709 IRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* thread, char* name, char* message))
 710   // lookup exception klass
 711   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 712   if (ProfileTraps) {
 713     if (s == vmSymbols::java_lang_ArithmeticException()) {
 714       note_trap(thread, Deoptimization::Reason_div0_check, CHECK);
 715     } else if (s == vmSymbols::java_lang_NullPointerException()) {
 716       note_trap(thread, Deoptimization::Reason_null_check, CHECK);
 717     }
 718   }
 719   // create exception
 720   Handle exception = Exceptions::new_exception(thread, s, message);
 721   thread->set_vm_result(exception());
 722 IRT_END
 723 
 724 
 725 IRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* thread, char* name, oopDesc* obj))
 726   ResourceMark rm(thread);
 727   const char* klass_name = obj->klass()->external_name();
 728   // lookup exception klass
 729   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 730   if (ProfileTraps) {
 731     note_trap(thread, Deoptimization::Reason_class_check, CHECK);
 732   }
 733   // create exception, with klass name as detail message
 734   Handle exception = Exceptions::new_exception(thread, s, klass_name);
 735   thread->set_vm_result(exception());
 736 IRT_END
 737 
 738 
 739 IRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* thread, char* name, jint index))
 740   char message[jintAsStringSize];
 741   // lookup exception klass
 742   TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
 743   if (ProfileTraps) {
 744     note_trap(thread, Deoptimization::Reason_range_check, CHECK);
 745   }
 746   // create exception
 747   sprintf(message, "%d", index);
 748   THROW_MSG(s, message);
 749 IRT_END
 750 
 751 IRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
 752   JavaThread* thread, oopDesc* obj))
 753 
 754   ResourceMark rm(thread);
 755   char* message = SharedRuntime::generate_class_cast_message(
 756     thread, obj->klass());
 757 
 758   if (ProfileTraps) {
 759     note_trap(thread, Deoptimization::Reason_class_check, CHECK);
 760   }
 761 
 762   // create exception
 763   THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
 764 IRT_END
 765 
 766 // exception_handler_for_exception(...) returns the continuation address,
 767 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
 768 // The exception oop is returned to make sure it is preserved over GC (it
 769 // is only on the stack if the exception was thrown explicitly via athrow).
 770 // During this operation, the expression stack contains the values for the
 771 // bci where the exception happened. If the exception was propagated back
 772 // from a call, the expression stack contains the values for the bci at the
 773 // invoke w/o arguments (i.e., as if one were inside the call).
 774 IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* thread, oopDesc* exception))
 775 
 776   Handle             h_exception(thread, exception);
 777   methodHandle       h_method   (thread, method(thread));
 778   constantPoolHandle h_constants(thread, h_method->constants());
 779   bool               should_repeat;
 780   int                handler_bci;
 781   int                current_bci = bci(thread);
 782 
 783   if (thread->frames_to_pop_failed_realloc() > 0) {
 784     // Allocation of scalar replaced object used in this frame
 785     // failed. Unconditionally pop the frame.
 786     thread->dec_frames_to_pop_failed_realloc();
 787     thread->set_vm_result(h_exception());
 788     // If the method is synchronized we already unlocked the monitor
 789     // during deoptimization so the interpreter needs to skip it when
 790     // the frame is popped.
 791     thread->set_do_not_unlock_if_synchronized(true);
 792 #ifdef CC_INTERP
 793     return (address) -1;
 794 #else
 795     return Interpreter::remove_activation_entry();
 796 #endif
 797   }
 798 
 799   // Need to do this check first since when _do_not_unlock_if_synchronized
 800   // is set, we don't want to trigger any classloading which may make calls
 801   // into java, or surprisingly find a matching exception handler for bci 0
 802   // since at this moment the method hasn't been "officially" entered yet.
 803   if (thread->do_not_unlock_if_synchronized()) {
 804     ResourceMark rm;
 805     assert(current_bci == 0,  "bci isn't zero for do_not_unlock_if_synchronized");
 806     thread->set_vm_result(exception);
 807 #ifdef CC_INTERP
 808     return (address) -1;
 809 #else
 810     return Interpreter::remove_activation_entry();
 811 #endif
 812   }
 813 
 814   do {
 815     should_repeat = false;
 816 
 817     // assertions
 818 #ifdef ASSERT
 819     assert(h_exception.not_null(), "NULL exceptions should be handled by athrow");
 820     // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
 821     if (!(h_exception->is_a(SystemDictionary::Throwable_klass()))) {
 822       if (ExitVMOnVerifyError) vm_exit(-1);
 823       ShouldNotReachHere();
 824     }
 825 #endif
 826 
 827     // tracing
 828     if (log_is_enabled(Info, exceptions)) {
 829       ResourceMark rm(thread);
 830       stringStream tempst;
 831       tempst.print("interpreter method <%s>\n"
 832                    " at bci %d for thread " INTPTR_FORMAT,
 833                    h_method->print_value_string(), current_bci, p2i(thread));
 834       Exceptions::log_exception(h_exception, tempst);
 835     }
 836 // Don't go paging in something which won't be used.
 837 //     else if (extable->length() == 0) {
 838 //       // disabled for now - interpreter is not using shortcut yet
 839 //       // (shortcut is not to call runtime if we have no exception handlers)
 840 //       // warning("performance bug: should not call runtime if method has no exception handlers");
 841 //     }
 842     // for AbortVMOnException flag
 843     Exceptions::debug_check_abort(h_exception);
 844 
 845     // exception handler lookup
 846     Klass* klass = h_exception->klass();
 847     handler_bci = Method::fast_exception_handler_bci_for(h_method, klass, current_bci, THREAD);
 848     if (HAS_PENDING_EXCEPTION) {
 849       // We threw an exception while trying to find the exception handler.
 850       // Transfer the new exception to the exception handle which will
 851       // be set into thread local storage, and do another lookup for an
 852       // exception handler for this exception, this time starting at the
 853       // BCI of the exception handler which caused the exception to be
 854       // thrown (bug 4307310).
 855       h_exception = Handle(THREAD, PENDING_EXCEPTION);
 856       CLEAR_PENDING_EXCEPTION;
 857       if (handler_bci >= 0) {
 858         current_bci = handler_bci;
 859         should_repeat = true;
 860       }
 861     }
 862   } while (should_repeat == true);
 863 
 864 #if INCLUDE_JVMCI
 865   if (EnableJVMCI && h_method->method_data() != NULL) {
 866     ResourceMark rm(thread);
 867     ProfileData* pdata = h_method->method_data()->allocate_bci_to_data(current_bci, NULL);
 868     if (pdata != NULL && pdata->is_BitData()) {
 869       BitData* bit_data = (BitData*) pdata;
 870       bit_data->set_exception_seen();
 871     }
 872   }
 873 #endif
 874 
 875   // notify JVMTI of an exception throw; JVMTI will detect if this is a first
 876   // time throw or a stack unwinding throw and accordingly notify the debugger
 877   if (JvmtiExport::can_post_on_exceptions()) {
 878     JvmtiExport::post_exception_throw(thread, h_method(), bcp(thread), h_exception());
 879   }
 880 
 881 #ifdef CC_INTERP
 882   address continuation = (address)(intptr_t) handler_bci;
 883 #else
 884   address continuation = NULL;
 885 #endif
 886   address handler_pc = NULL;
 887   if (handler_bci < 0 || !thread->reguard_stack((address) &continuation)) {
 888     // Forward exception to callee (leaving bci/bcp untouched) because (a) no
 889     // handler in this method, or (b) after a stack overflow there is not yet
 890     // enough stack space available to reprotect the stack.
 891 #ifndef CC_INTERP
 892     continuation = Interpreter::remove_activation_entry();
 893 #endif
 894 #if COMPILER2_OR_JVMCI
 895     // Count this for compilation purposes
 896     h_method->interpreter_throwout_increment(THREAD);
 897 #endif
 898   } else {
 899     // handler in this method => change bci/bcp to handler bci/bcp and continue there
 900     handler_pc = h_method->code_base() + handler_bci;
 901 #ifndef CC_INTERP
 902     set_bcp_and_mdp(handler_pc, thread);
 903     continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
 904 #endif
 905   }
 906   // notify debugger of an exception catch
 907   // (this is good for exceptions caught in native methods as well)
 908   if (JvmtiExport::can_post_on_exceptions()) {
 909     JvmtiExport::notice_unwind_due_to_exception(thread, h_method(), handler_pc, h_exception(), (handler_pc != NULL));
 910   }
 911 
 912   thread->set_vm_result(h_exception());
 913   return continuation;
 914 IRT_END
 915 
 916 
 917 IRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* thread))
 918   assert(thread->has_pending_exception(), "must only ne called if there's an exception pending");
 919   // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
 920 IRT_END
 921 
 922 
 923 IRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* thread))
 924   THROW(vmSymbols::java_lang_AbstractMethodError());
 925 IRT_END
 926 
 927 
 928 IRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* thread))
 929   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 930 IRT_END
 931 
 932 
 933 //------------------------------------------------------------------------------------------------------------------------
 934 // Fields
 935 //
 936 
 937 void InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code bytecode) {
 938   Thread* THREAD = thread;
 939   // resolve field
 940   fieldDescriptor info;
 941   constantPoolHandle pool(thread, method(thread)->constants());
 942   methodHandle m(thread, method(thread));
 943   bool is_put    = (bytecode == Bytecodes::_putfield  || bytecode == Bytecodes::_nofast_putfield ||
 944                     bytecode == Bytecodes::_putstatic || bytecode == Bytecodes::_vwithfield);
 945   bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
 946   bool is_value  = bytecode == Bytecodes::_vwithfield;
 947 
 948   {
 949     JvmtiHideSingleStepping jhss(thread);
 950     LinkResolver::resolve_field_access(info, pool, get_index_u2_cpcache(thread, bytecode),
 951                                        m, bytecode, CHECK);
 952   } // end JvmtiHideSingleStepping
 953 
 954   // check if link resolution caused cpCache to be updated
 955   ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread);
 956   if (cp_cache_entry->is_resolved(bytecode)) return;
 957 
 958   // compute auxiliary field attributes
 959   TosState state  = as_TosState(info.field_type());
 960 
 961   // Resolution of put instructions on final fields is delayed. That is required so that
 962   // exceptions are thrown at the correct place (when the instruction is actually invoked).
 963   // If we do not resolve an instruction in the current pass, leaving the put_code
 964   // set to zero will cause the next put instruction to the same field to reresolve.
 965 
 966   // Resolution of put instructions to final instance fields with invalid updates (i.e.,
 967   // to final instance fields with updates originating from a method different than <init>)
 968   // is inhibited. A putfield instruction targeting an instance final field must throw
 969   // an IllegalAccessError if the instruction is not in an instance
 970   // initializer method <init>. If resolution were not inhibited, a putfield
 971   // in an initializer method could be resolved in the initializer. Subsequent
 972   // putfield instructions to the same field would then use cached information.
 973   // As a result, those instructions would not pass through the VM. That is,
 974   // checks in resolve_field_access() would not be executed for those instructions
 975   // and the required IllegalAccessError would not be thrown.
 976   //
 977   // Also, we need to delay resolving getstatic and putstatic instructions until the
 978   // class is initialized.  This is required so that access to the static
 979   // field will call the initialization function every time until the class
 980   // is completely initialized ala. in 2.17.5 in JVM Specification.
 981   InstanceKlass* klass = InstanceKlass::cast(info.field_holder());
 982   bool uninitialized_static = is_static && !klass->is_initialized();
 983   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 984                                       info.has_initialized_final_update();
 985   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 986 
 987   Bytecodes::Code get_code = (Bytecodes::Code)0;
 988   Bytecodes::Code put_code = (Bytecodes::Code)0;
 989   if (!uninitialized_static) {
 990     if (is_static) {
 991       get_code = Bytecodes::_getstatic;
 992     } else {
 993       get_code = Bytecodes::_getfield;
 994     }
 995     if (is_put && is_value) {
 996         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_vwithfield);
 997     } else if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 998         put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 999     }
1000   }
1001 
1002   cp_cache_entry->set_field(
1003     get_code,
1004     put_code,
1005     info.field_holder(),
1006     info.index(),
1007     info.offset(),
1008     state,
1009     info.access_flags().is_final(),
1010     info.access_flags().is_volatile(),
1011     info.is_flatten(),
1012     pool->pool_holder()
1013   );
1014 }
1015 
1016 
1017 //------------------------------------------------------------------------------------------------------------------------
1018 // Synchronization
1019 //
1020 // The interpreter's synchronization code is factored out so that it can
1021 // be shared by method invocation and synchronized blocks.
1022 //%note synchronization_3
1023 
1024 //%note monitor_1
1025 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
1026 #ifdef ASSERT
1027   thread->last_frame().interpreter_frame_verify_monitor(elem);
1028 #endif
1029   if (PrintBiasedLockingStatistics) {
1030     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
1031   }
1032   Handle h_obj(thread, elem->obj());
1033   assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
1034          "must be NULL or an object");
1035   if (UseBiasedLocking) {
1036     // Retry fast entry if bias is revoked to avoid unnecessary inflation
1037     ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
1038   } else {
1039     ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
1040   }
1041   assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
1042          "must be NULL or an object");
1043 #ifdef ASSERT
1044   thread->last_frame().interpreter_frame_verify_monitor(elem);
1045 #endif
1046 IRT_END
1047 
1048 
1049 //%note monitor_1
1050 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
1051 #ifdef ASSERT
1052   thread->last_frame().interpreter_frame_verify_monitor(elem);
1053 #endif
1054   Handle h_obj(thread, elem->obj());
1055   assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
1056          "must be NULL or an object");
1057   if (elem == NULL || h_obj()->is_unlocked()) {
1058     THROW(vmSymbols::java_lang_IllegalMonitorStateException());
1059   }
1060   ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);
1061   // Free entry. This must be done here, since a pending exception might be installed on
1062   // exit. If it is not cleared, the exception handling code will try to unlock the monitor again.
1063   elem->set_obj(NULL);
1064 #ifdef ASSERT
1065   thread->last_frame().interpreter_frame_verify_monitor(elem);
1066 #endif
1067 IRT_END
1068 
1069 
1070 IRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* thread))
1071   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
1072 IRT_END
1073 
1074 
1075 IRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* thread))
1076   // Returns an illegal exception to install into the current thread. The
1077   // pending_exception flag is cleared so normal exception handling does not
1078   // trigger. Any current installed exception will be overwritten. This
1079   // method will be called during an exception unwind.
1080 
1081   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
1082   Handle exception(thread, thread->vm_result());
1083   assert(exception() != NULL, "vm result should be set");
1084   thread->set_vm_result(NULL); // clear vm result before continuing (may cause memory leaks and assert failures)
1085   if (!exception->is_a(SystemDictionary::ThreadDeath_klass())) {
1086     exception = get_preinitialized_exception(
1087                        SystemDictionary::IllegalMonitorStateException_klass(),
1088                        CATCH);
1089   }
1090   thread->set_vm_result(exception());
1091 IRT_END
1092 
1093 
1094 //------------------------------------------------------------------------------------------------------------------------
1095 // Invokes
1096 
1097 IRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* thread, Method* method, address bcp))
1098   return method->orig_bytecode_at(method->bci_from(bcp));
1099 IRT_END
1100 
1101 IRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* thread, Method* method, address bcp, Bytecodes::Code new_code))
1102   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
1103 IRT_END
1104 
1105 IRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* thread, Method* method, address bcp))
1106   JvmtiExport::post_raw_breakpoint(thread, method, bcp);
1107 IRT_END
1108 
1109 void InterpreterRuntime::resolve_invoke(JavaThread* thread, Bytecodes::Code bytecode) {
1110   Thread* THREAD = thread;
1111   // extract receiver from the outgoing argument list if necessary
1112   Handle receiver(thread, NULL);
1113   if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface ||
1114       bytecode == Bytecodes::_invokespecial) {
1115     ResourceMark rm(thread);
1116     methodHandle m (thread, method(thread));
1117     Bytecode_invoke call(m, bci(thread));
1118     Symbol* signature = call.signature();
1119     receiver = Handle(thread,
1120                   thread->last_frame().interpreter_callee_receiver(signature));
1121     assert(Universe::heap()->is_in_reserved_or_null(receiver())
1122            || VTBuffer::is_in_vt_buffer(receiver()),
1123            "sanity check");
1124     assert(receiver.is_null() ||
1125            !Universe::heap()->is_in_reserved(receiver->klass()),
1126            "sanity check");
1127   }
1128 
1129   // resolve method
1130   CallInfo info;
1131   constantPoolHandle pool(thread, method(thread)->constants());
1132 
1133   {
1134     JvmtiHideSingleStepping jhss(thread);
1135     LinkResolver::resolve_invoke(info, receiver, pool,
1136                                  get_index_u2_cpcache(thread, bytecode), bytecode,
1137                                  CHECK);
1138     if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
1139       int retry_count = 0;
1140       while (info.resolved_method()->is_old()) {
1141         // It is very unlikely that method is redefined more than 100 times
1142         // in the middle of resolve. If it is looping here more than 100 times
1143         // means then there could be a bug here.
1144         guarantee((retry_count++ < 100),
1145                   "Could not resolve to latest version of redefined method");
1146         // method is redefined in the middle of resolve so re-try.
1147         LinkResolver::resolve_invoke(info, receiver, pool,
1148                                      get_index_u2_cpcache(thread, bytecode), bytecode,
1149                                      CHECK);
1150       }
1151     }
1152   } // end JvmtiHideSingleStepping
1153 
1154   // check if link resolution caused cpCache to be updated
1155   ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread);
1156   if (cp_cache_entry->is_resolved(bytecode)) return;
1157 
1158 #ifdef ASSERT
1159   if (bytecode == Bytecodes::_invokeinterface) {
1160     if (info.resolved_method()->method_holder() ==
1161                                             SystemDictionary::Object_klass()) {
1162       // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
1163       // (see also CallInfo::set_interface for details)
1164       assert(info.call_kind() == CallInfo::vtable_call ||
1165              info.call_kind() == CallInfo::direct_call, "");
1166       methodHandle rm = info.resolved_method();
1167       assert(rm->is_final() || info.has_vtable_index(),
1168              "should have been set already");
1169     } else if (!info.resolved_method()->has_itable_index()) {
1170       // Resolved something like CharSequence.toString.  Use vtable not itable.
1171       assert(info.call_kind() != CallInfo::itable_call, "");
1172     } else {
1173       // Setup itable entry
1174       assert(info.call_kind() == CallInfo::itable_call, "");
1175       int index = info.resolved_method()->itable_index();
1176       assert(info.itable_index() == index, "");
1177     }
1178   } else if (bytecode == Bytecodes::_invokespecial) {
1179     assert(info.call_kind() == CallInfo::direct_call, "must be direct call");
1180   } else {
1181     assert(info.call_kind() == CallInfo::direct_call ||
1182            info.call_kind() == CallInfo::vtable_call, "");
1183   }
1184 #endif
1185   // Get sender or sender's host_klass, and only set cpCache entry to resolved if
1186   // it is not an interface.  The receiver for invokespecial calls within interface
1187   // methods must be checked for every call.
1188   InstanceKlass* sender = pool->pool_holder();
1189   sender = sender->is_anonymous() ? sender->host_klass() : sender;
1190 
1191   switch (info.call_kind()) {
1192   case CallInfo::direct_call:
1193     cp_cache_entry->set_direct_call(
1194       bytecode,
1195       info.resolved_method(),
1196       sender->is_interface());
1197     break;
1198   case CallInfo::vtable_call:
1199     cp_cache_entry->set_vtable_call(
1200       bytecode,
1201       info.resolved_method(),
1202       info.vtable_index());
1203     break;
1204   case CallInfo::itable_call:
1205     cp_cache_entry->set_itable_call(
1206       bytecode,
1207       info.resolved_method(),
1208       info.itable_index());
1209     break;
1210   default:  ShouldNotReachHere();
1211   }
1212 }
1213 
1214 
1215 // First time execution:  Resolve symbols, create a permanent MethodType object.
1216 void InterpreterRuntime::resolve_invokehandle(JavaThread* thread) {
1217   Thread* THREAD = thread;
1218   const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
1219 
1220   // resolve method
1221   CallInfo info;
1222   constantPoolHandle pool(thread, method(thread)->constants());
1223   {
1224     JvmtiHideSingleStepping jhss(thread);
1225     LinkResolver::resolve_invoke(info, Handle(), pool,
1226                                  get_index_u2_cpcache(thread, bytecode), bytecode,
1227                                  CHECK);
1228   } // end JvmtiHideSingleStepping
1229 
1230   ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread);
1231   cp_cache_entry->set_method_handle(pool, info);
1232 }
1233 
1234 // First time execution:  Resolve symbols, create a permanent CallSite object.
1235 void InterpreterRuntime::resolve_invokedynamic(JavaThread* thread) {
1236   Thread* THREAD = thread;
1237   const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
1238 
1239   //TO DO: consider passing BCI to Java.
1240   //  int caller_bci = method(thread)->bci_from(bcp(thread));
1241 
1242   // resolve method
1243   CallInfo info;
1244   constantPoolHandle pool(thread, method(thread)->constants());
1245   int index = get_index_u4(thread, bytecode);
1246   {
1247     JvmtiHideSingleStepping jhss(thread);
1248     LinkResolver::resolve_invoke(info, Handle(), pool,
1249                                  index, bytecode, CHECK);
1250   } // end JvmtiHideSingleStepping
1251 
1252   ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
1253   cp_cache_entry->set_dynamic_call(pool, info);
1254 }
1255 
1256 // This function is the interface to the assembly code. It returns the resolved
1257 // cpCache entry.  This doesn't safepoint, but the helper routines safepoint.
1258 // This function will check for redefinition!
1259 IRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* thread, Bytecodes::Code bytecode)) {
1260   switch (bytecode) {
1261   case Bytecodes::_getstatic:
1262   case Bytecodes::_putstatic:
1263   case Bytecodes::_getfield:
1264   case Bytecodes::_putfield:
1265   case Bytecodes::_vwithfield:
1266     resolve_get_put(thread, bytecode);
1267     break;
1268   case Bytecodes::_invokevirtual:
1269   case Bytecodes::_invokespecial:
1270   case Bytecodes::_invokestatic:
1271   case Bytecodes::_invokeinterface:
1272     resolve_invoke(thread, bytecode);
1273     break;
1274   case Bytecodes::_invokehandle:
1275     resolve_invokehandle(thread);
1276     break;
1277   case Bytecodes::_invokedynamic:
1278     resolve_invokedynamic(thread);
1279     break;
1280   default:
1281     fatal("unexpected bytecode: %s", Bytecodes::name(bytecode));
1282     break;
1283   }
1284 }
1285 IRT_END
1286 
1287 //------------------------------------------------------------------------------------------------------------------------
1288 // Miscellaneous
1289 
1290 
1291 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) {
1292   nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
1293   assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
1294   if (branch_bcp != NULL && nm != NULL) {
1295     // This was a successful request for an OSR nmethod.  Because
1296     // frequency_counter_overflow_inner ends with a safepoint check,
1297     // nm could have been unloaded so look it up again.  It's unsafe
1298     // to examine nm directly since it might have been freed and used
1299     // for something else.
1300     frame fr = thread->last_frame();
1301     Method* method =  fr.interpreter_frame_method();
1302     int bci = method->bci_from(fr.interpreter_frame_bcp());
1303     nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
1304   }
1305 #ifndef PRODUCT
1306   if (TraceOnStackReplacement) {
1307     if (nm != NULL) {
1308       tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", p2i(nm->osr_entry()));
1309       nm->print();
1310     }
1311   }
1312 #endif
1313   return nm;
1314 }
1315 
1316 IRT_ENTRY(nmethod*,
1317           InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp))
1318   // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
1319   // flag, in case this method triggers classloading which will call into Java.
1320   UnlockFlagSaver fs(thread);
1321 
1322   frame fr = thread->last_frame();
1323   assert(fr.is_interpreted_frame(), "must come from interpreter");
1324   methodHandle method(thread, fr.interpreter_frame_method());
1325   const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : InvocationEntryBci;
1326   const int bci = branch_bcp != NULL ? method->bci_from(fr.interpreter_frame_bcp()) : InvocationEntryBci;
1327 
1328   assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
1329   nmethod* osr_nm = CompilationPolicy::policy()->event(method, method, branch_bci, bci, CompLevel_none, NULL, thread);
1330   assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
1331 
1332   if (osr_nm != NULL) {
1333     // We may need to do on-stack replacement which requires that no
1334     // monitors in the activation are biased because their
1335     // BasicObjectLocks will need to migrate during OSR. Force
1336     // unbiasing of all monitors in the activation now (even though
1337     // the OSR nmethod might be invalidated) because we don't have a
1338     // safepoint opportunity later once the migration begins.
1339     if (UseBiasedLocking) {
1340       ResourceMark rm;
1341       GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1342       for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
1343            kptr < fr.interpreter_frame_monitor_begin();
1344            kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
1345         if( kptr->obj() != NULL ) {
1346           objects_to_revoke->append(Handle(THREAD, kptr->obj()));
1347         }
1348       }
1349       BiasedLocking::revoke(objects_to_revoke);
1350     }
1351   }
1352   return osr_nm;
1353 IRT_END
1354 
1355 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
1356   assert(ProfileInterpreter, "must be profiling interpreter");
1357   int bci = method->bci_from(cur_bcp);
1358   MethodData* mdo = method->method_data();
1359   if (mdo == NULL)  return 0;
1360   return mdo->bci_to_di(bci);
1361 IRT_END
1362 
1363 IRT_ENTRY(void, InterpreterRuntime::profile_method(JavaThread* thread))
1364   // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
1365   // flag, in case this method triggers classloading which will call into Java.
1366   UnlockFlagSaver fs(thread);
1367 
1368   assert(ProfileInterpreter, "must be profiling interpreter");
1369   frame fr = thread->last_frame();
1370   assert(fr.is_interpreted_frame(), "must come from interpreter");
1371   methodHandle method(thread, fr.interpreter_frame_method());
1372   Method::build_interpreter_method_data(method, THREAD);
1373   if (HAS_PENDING_EXCEPTION) {
1374     assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1375     CLEAR_PENDING_EXCEPTION;
1376     // and fall through...
1377   }
1378 IRT_END
1379 
1380 
1381 #ifdef ASSERT
1382 IRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
1383   assert(ProfileInterpreter, "must be profiling interpreter");
1384 
1385   MethodData* mdo = method->method_data();
1386   assert(mdo != NULL, "must not be null");
1387 
1388   int bci = method->bci_from(bcp);
1389 
1390   address mdp2 = mdo->bci_to_dp(bci);
1391   if (mdp != mdp2) {
1392     ResourceMark rm;
1393     ResetNoHandleMark rnm; // In a LEAF entry.
1394     HandleMark hm;
1395     tty->print_cr("FAILED verify : actual mdp %p   expected mdp %p @ bci %d", mdp, mdp2, bci);
1396     int current_di = mdo->dp_to_di(mdp);
1397     int expected_di  = mdo->dp_to_di(mdp2);
1398     tty->print_cr("  actual di %d   expected di %d", current_di, expected_di);
1399     int expected_approx_bci = mdo->data_at(expected_di)->bci();
1400     int approx_bci = -1;
1401     if (current_di >= 0) {
1402       approx_bci = mdo->data_at(current_di)->bci();
1403     }
1404     tty->print_cr("  actual bci is %d  expected bci %d", approx_bci, expected_approx_bci);
1405     mdo->print_on(tty);
1406     method->print_codes();
1407   }
1408   assert(mdp == mdp2, "wrong mdp");
1409 IRT_END
1410 #endif // ASSERT
1411 
1412 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci))
1413   assert(ProfileInterpreter, "must be profiling interpreter");
1414   ResourceMark rm(thread);
1415   HandleMark hm(thread);
1416   frame fr = thread->last_frame();
1417   assert(fr.is_interpreted_frame(), "must come from interpreter");
1418   MethodData* h_mdo = fr.interpreter_frame_method()->method_data();
1419 
1420   // Grab a lock to ensure atomic access to setting the return bci and
1421   // the displacement.  This can block and GC, invalidating all naked oops.
1422   MutexLocker ml(RetData_lock);
1423 
1424   // ProfileData is essentially a wrapper around a derived oop, so we
1425   // need to take the lock before making any ProfileData structures.
1426   ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp()));
1427   guarantee(data != NULL, "profile data must be valid");
1428   RetData* rdata = data->as_RetData();
1429   address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
1430   fr.interpreter_frame_set_mdp(new_mdp);
1431 IRT_END
1432 
1433 IRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* thread, Method* m))
1434   MethodCounters* mcs = Method::build_method_counters(m, thread);
1435   if (HAS_PENDING_EXCEPTION) {
1436     assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1437     CLEAR_PENDING_EXCEPTION;
1438   }
1439   return mcs;
1440 IRT_END
1441 
1442 
1443 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread))
1444   // We used to need an explict preserve_arguments here for invoke bytecodes. However,
1445   // stack traversal automatically takes care of preserving arguments for invoke, so
1446   // this is no longer needed.
1447 
1448   // IRT_END does an implicit safepoint check, hence we are guaranteed to block
1449   // if this is called during a safepoint
1450 
1451   if (JvmtiExport::should_post_single_step()) {
1452     // We are called during regular safepoints and when the VM is
1453     // single stepping. If any thread is marked for single stepping,
1454     // then we may have JVMTI work to do.
1455     JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread));
1456   }
1457 IRT_END
1458 
1459 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj,
1460 ConstantPoolCacheEntry *cp_entry))
1461 
1462   // check the access_flags for the field in the klass
1463 
1464   InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1465   int index = cp_entry->field_index();
1466   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1467 
1468   bool is_static = (obj == NULL);
1469   HandleMark hm(thread);
1470 
1471   Handle h_obj;
1472   if (!is_static) {
1473     // non-static field accessors have an object, but we need a handle
1474     h_obj = Handle(thread, obj);
1475   }
1476   InstanceKlass* cp_entry_f1 = InstanceKlass::cast(cp_entry->f1_as_klass());
1477   jfieldID fid = jfieldIDWorkaround::to_jfieldID(cp_entry_f1, cp_entry->f2_as_index(), is_static);
1478   JvmtiExport::post_field_access(thread, method(thread), bcp(thread), cp_entry_f1, h_obj, fid);
1479 IRT_END
1480 
1481 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread,
1482   oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value))
1483 
1484   Klass* k = cp_entry->f1_as_klass();
1485 
1486   // check the access_flags for the field in the klass
1487   InstanceKlass* ik = InstanceKlass::cast(k);
1488   int index = cp_entry->field_index();
1489   // bail out if field modifications are not watched
1490   if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1491 
1492   char sig_type = '\0';
1493 
1494   switch(cp_entry->flag_state()) {
1495     case btos: sig_type = 'B'; break;
1496     case ztos: sig_type = 'Z'; break;
1497     case ctos: sig_type = 'C'; break;
1498     case stos: sig_type = 'S'; break;
1499     case itos: sig_type = 'I'; break;
1500     case ftos: sig_type = 'F'; break;
1501     case atos: sig_type = 'L'; break;
1502     case ltos: sig_type = 'J'; break;
1503     case dtos: sig_type = 'D'; break;
1504     default:  ShouldNotReachHere(); return;
1505   }
1506   bool is_static = (obj == NULL);
1507 
1508   HandleMark hm(thread);
1509   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static);
1510   jvalue fvalue;
1511 #ifdef _LP64
1512   fvalue = *value;
1513 #else
1514   // Long/double values are stored unaligned and also noncontiguously with
1515   // tagged stacks.  We can't just do a simple assignment even in the non-
1516   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1517   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1518   // We assume that the two halves of longs/doubles are stored in interpreter
1519   // stack slots in platform-endian order.
1520   jlong_accessor u;
1521   jint* newval = (jint*)value;
1522   u.words[0] = newval[0];
1523   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1524   fvalue.j = u.long_value;
1525 #endif // _LP64
1526 
1527   Handle h_obj;
1528   if (!is_static) {
1529     // non-static field accessors have an object, but we need a handle
1530     h_obj = Handle(thread, obj);
1531   }
1532 
1533   JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), ik, h_obj,
1534                                            fid, sig_type, &fvalue);
1535 IRT_END
1536 
1537 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread))
1538   JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1539 IRT_END
1540 
1541 
1542 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread))
1543   JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1544 IRT_END
1545 
1546 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1547 {
1548   return (Interpreter::contains(pc) ? 1 : 0);
1549 }
1550 IRT_END
1551 
1552 
1553 // Implementation of SignatureHandlerLibrary
1554 
1555 #ifndef SHARING_FAST_NATIVE_FINGERPRINTS
1556 // Dummy definition (else normalization method is defined in CPU
1557 // dependant code)
1558 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) {
1559   return fingerprint;
1560 }
1561 #endif
1562 
1563 address SignatureHandlerLibrary::set_handler_blob() {
1564   BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1565   if (handler_blob == NULL) {
1566     return NULL;
1567   }
1568   address handler = handler_blob->code_begin();
1569   _handler_blob = handler_blob;
1570   _handler = handler;
1571   return handler;
1572 }
1573 
1574 void SignatureHandlerLibrary::initialize() {
1575   if (_fingerprints != NULL) {
1576     return;
1577   }
1578   if (set_handler_blob() == NULL) {
1579     vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1580   }
1581 
1582   BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1583                                       SignatureHandlerLibrary::buffer_size);
1584   _buffer = bb->code_begin();
1585 
1586   _fingerprints = new(ResourceObj::C_HEAP, mtCode)GrowableArray<uint64_t>(32, true);
1587   _handlers     = new(ResourceObj::C_HEAP, mtCode)GrowableArray<address>(32, true);
1588 }
1589 
1590 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1591   address handler   = _handler;
1592   int     insts_size = buffer->pure_insts_size();
1593   if (handler + insts_size > _handler_blob->code_end()) {
1594     // get a new handler blob
1595     handler = set_handler_blob();
1596   }
1597   if (handler != NULL) {
1598     memcpy(handler, buffer->insts_begin(), insts_size);
1599     pd_set_handler(handler);
1600     ICache::invalidate_range(handler, insts_size);
1601     _handler = handler + insts_size;
1602   }
1603   return handler;
1604 }
1605 
1606 void SignatureHandlerLibrary::add(const methodHandle& method) {
1607   if (method->signature_handler() == NULL) {
1608     // use slow signature handler if we can't do better
1609     int handler_index = -1;
1610     // check if we can use customized (fast) signature handler
1611     if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) {
1612       // use customized signature handler
1613       MutexLocker mu(SignatureHandlerLibrary_lock);
1614       // make sure data structure is initialized
1615       initialize();
1616       // lookup method signature's fingerprint
1617       uint64_t fingerprint = Fingerprinter(method).fingerprint();
1618       // allow CPU dependant code to optimize the fingerprints for the fast handler
1619       fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1620       handler_index = _fingerprints->find(fingerprint);
1621       // create handler if necessary
1622       if (handler_index < 0) {
1623         ResourceMark rm;
1624         ptrdiff_t align_offset = align_up(_buffer, CodeEntryAlignment) - (address)_buffer;
1625         CodeBuffer buffer((address)(_buffer + align_offset),
1626                           SignatureHandlerLibrary::buffer_size - align_offset);
1627         InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1628         // copy into code heap
1629         address handler = set_handler(&buffer);
1630         if (handler == NULL) {
1631           // use slow signature handler (without memorizing it in the fingerprints)
1632         } else {
1633           // debugging suppport
1634           if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) {
1635             ttyLocker ttyl;
1636             tty->cr();
1637             tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1638                           _handlers->length(),
1639                           (method->is_static() ? "static" : "receiver"),
1640                           method->name_and_sig_as_C_string(),
1641                           fingerprint,
1642                           buffer.insts_size());
1643             if (buffer.insts_size() > 0) {
1644               Disassembler::decode(handler, handler + buffer.insts_size());
1645             }
1646 #ifndef PRODUCT
1647             address rh_begin = Interpreter::result_handler(method()->result_type());
1648             if (CodeCache::contains(rh_begin)) {
1649               // else it might be special platform dependent values
1650               tty->print_cr(" --- associated result handler ---");
1651               address rh_end = rh_begin;
1652               while (*(int*)rh_end != 0) {
1653                 rh_end += sizeof(int);
1654               }
1655               Disassembler::decode(rh_begin, rh_end);
1656             } else {
1657               tty->print_cr(" associated result handler: " PTR_FORMAT, p2i(rh_begin));
1658             }
1659 #endif
1660           }
1661           // add handler to library
1662           _fingerprints->append(fingerprint);
1663           _handlers->append(handler);
1664           // set handler index
1665           assert(_fingerprints->length() == _handlers->length(), "sanity check");
1666           handler_index = _fingerprints->length() - 1;
1667         }
1668       }
1669       // Set handler under SignatureHandlerLibrary_lock
1670       if (handler_index < 0) {
1671         // use generic signature handler
1672         method->set_signature_handler(Interpreter::slow_signature_handler());
1673       } else {
1674         // set handler
1675         method->set_signature_handler(_handlers->at(handler_index));
1676       }
1677     } else {
1678       CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1679       // use generic signature handler
1680       method->set_signature_handler(Interpreter::slow_signature_handler());
1681     }
1682   }
1683 #ifdef ASSERT
1684   int handler_index = -1;
1685   int fingerprint_index = -2;
1686   {
1687     // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1688     // in any way if accessed from multiple threads. To avoid races with another
1689     // thread which may change the arrays in the above, mutex protected block, we
1690     // have to protect this read access here with the same mutex as well!
1691     MutexLocker mu(SignatureHandlerLibrary_lock);
1692     if (_handlers != NULL) {
1693       handler_index = _handlers->find(method->signature_handler());
1694       uint64_t fingerprint = Fingerprinter(method).fingerprint();
1695       fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1696       fingerprint_index = _fingerprints->find(fingerprint);
1697     }
1698   }
1699   assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1700          handler_index == fingerprint_index, "sanity check");
1701 #endif // ASSERT
1702 }
1703 
1704 void SignatureHandlerLibrary::add(uint64_t fingerprint, address handler) {
1705   int handler_index = -1;
1706   // use customized signature handler
1707   MutexLocker mu(SignatureHandlerLibrary_lock);
1708   // make sure data structure is initialized
1709   initialize();
1710   fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint);
1711   handler_index = _fingerprints->find(fingerprint);
1712   // create handler if necessary
1713   if (handler_index < 0) {
1714     if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) {
1715       tty->cr();
1716       tty->print_cr("argument handler #%d at " PTR_FORMAT " for fingerprint " UINT64_FORMAT,
1717                     _handlers->length(),
1718                     p2i(handler),
1719                     fingerprint);
1720     }
1721     _fingerprints->append(fingerprint);
1722     _handlers->append(handler);
1723   } else {
1724     if (PrintSignatureHandlers) {
1725       tty->cr();
1726       tty->print_cr("duplicate argument handler #%d for fingerprint " UINT64_FORMAT "(old: " PTR_FORMAT ", new : " PTR_FORMAT ")",
1727                     _handlers->length(),
1728                     fingerprint,
1729                     p2i(_handlers->at(handler_index)),
1730                     p2i(handler));
1731     }
1732   }
1733 }
1734 
1735 
1736 BufferBlob*              SignatureHandlerLibrary::_handler_blob = NULL;
1737 address                  SignatureHandlerLibrary::_handler      = NULL;
1738 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL;
1739 GrowableArray<address>*  SignatureHandlerLibrary::_handlers     = NULL;
1740 address                  SignatureHandlerLibrary::_buffer       = NULL;
1741 
1742 
1743 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, Method* method))
1744   methodHandle m(thread, method);
1745   assert(m->is_native(), "sanity check");
1746   // lookup native function entry point if it doesn't exist
1747   bool in_base_library;
1748   if (!m->has_native_function()) {
1749     NativeLookup::lookup(m, in_base_library, CHECK);
1750   }
1751   // make sure signature handler is installed
1752   SignatureHandlerLibrary::add(m);
1753   // The interpreter entry point checks the signature handler first,
1754   // before trying to fetch the native entry point and klass mirror.
1755   // We must set the signature handler last, so that multiple processors
1756   // preparing the same method will be sure to see non-null entry & mirror.
1757 IRT_END
1758 
1759 #if defined(IA32) || defined(AMD64) || defined(ARM)
1760 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address))
1761   if (src_address == dest_address) {
1762     return;
1763   }
1764   ResetNoHandleMark rnm; // In a LEAF entry.
1765   HandleMark hm;
1766   ResourceMark rm;
1767   frame fr = thread->last_frame();
1768   assert(fr.is_interpreted_frame(), "");
1769   jint bci = fr.interpreter_frame_bci();
1770   methodHandle mh(thread, fr.interpreter_frame_method());
1771   Bytecode_invoke invoke(mh, bci);
1772   ArgumentSizeComputer asc(invoke.signature());
1773   int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1774   Copy::conjoint_jbytes(src_address, dest_address,
1775                        size_of_arguments * Interpreter::stackElementSize);
1776 IRT_END
1777 #endif
1778 
1779 #if INCLUDE_JVMTI
1780 // This is a support of the JVMTI PopFrame interface.
1781 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1782 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters.
1783 // The member_name argument is a saved reference (in local#0) to the member_name.
1784 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle.
1785 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated.
1786 IRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* thread, address member_name,
1787                                                             Method* method, address bcp))
1788   Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1789   if (code != Bytecodes::_invokestatic) {
1790     return;
1791   }
1792   ConstantPool* cpool = method->constants();
1793   int cp_index = Bytes::get_native_u2(bcp + 1) + ConstantPool::CPCACHE_INDEX_TAG;
1794   Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index));
1795   Symbol* mname = cpool->name_ref_at(cp_index);
1796 
1797   if (MethodHandles::has_member_arg(cname, mname)) {
1798     oop member_name_oop = (oop) member_name;
1799     if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) {
1800       // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated.
1801       member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop);
1802     }
1803     thread->set_vm_result(member_name_oop);
1804   } else {
1805     thread->set_vm_result(NULL);
1806   }
1807 IRT_END
1808 #endif // INCLUDE_JVMTI
1809 
1810 #ifndef PRODUCT
1811 // This must be a IRT_LEAF function because the interpreter must save registers on x86 to
1812 // call this, which changes rsp and makes the interpreter's expression stack not walkable.
1813 // The generated code still uses call_VM because that will set up the frame pointer for
1814 // bcp and method.
1815 IRT_LEAF(intptr_t, InterpreterRuntime::trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2))
1816   const frame f = thread->last_frame();
1817   assert(f.is_interpreted_frame(), "must be an interpreted frame");
1818   methodHandle mh(thread, f.interpreter_frame_method());
1819   BytecodeTracer::trace(mh, f.interpreter_frame_bcp(), tos, tos2);
1820   return preserve_this_value;
1821 IRT_END
1822 #endif // !PRODUCT