1 /*
   2  * Copyright (c) 1999, 2019, 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 "asm/codeBuffer.hpp"
  27 #include "c1/c1_CodeStubs.hpp"
  28 #include "c1/c1_Defs.hpp"
  29 #include "c1/c1_FrameMap.hpp"
  30 #include "c1/c1_LIRAssembler.hpp"
  31 #include "c1/c1_MacroAssembler.hpp"
  32 #include "c1/c1_Runtime1.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/vmSymbols.hpp"
  35 #include "code/codeBlob.hpp"
  36 #include "code/compiledIC.hpp"
  37 #include "code/pcDesc.hpp"
  38 #include "code/scopeDesc.hpp"
  39 #include "code/vtableStubs.hpp"
  40 #include "compiler/disassembler.hpp"
  41 #include "gc/shared/barrierSet.hpp"
  42 #include "gc/shared/c1/barrierSetC1.hpp"
  43 #include "gc/shared/collectedHeap.hpp"
  44 #include "interpreter/bytecode.hpp"
  45 #include "interpreter/interpreter.hpp"
  46 #include "jfr/support/jfrIntrinsics.hpp"
  47 #include "logging/log.hpp"
  48 #include "memory/allocation.inline.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "oops/access.inline.hpp"
  53 #include "oops/objArrayOop.inline.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/oop.inline.hpp"
  56 #include "oops/valueArrayKlass.hpp"
  57 #include "oops/valueArrayOop.inline.hpp"
  58 #include "runtime/atomic.hpp"
  59 #include "runtime/biasedLocking.hpp"
  60 #include "runtime/compilationPolicy.hpp"
  61 #include "runtime/fieldDescriptor.inline.hpp"
  62 #include "runtime/frame.inline.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/javaCalls.hpp"
  66 #include "runtime/sharedRuntime.hpp"
  67 #include "runtime/threadCritical.hpp"
  68 #include "runtime/vframe.inline.hpp"
  69 #include "runtime/vframeArray.hpp"
  70 #include "runtime/vm_version.hpp"
  71 #include "utilities/copy.hpp"
  72 #include "utilities/events.hpp"
  73 
  74 
  75 // Implementation of StubAssembler
  76 
  77 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
  78   _name = name;
  79   _must_gc_arguments = false;
  80   _frame_size = no_frame_size;
  81   _num_rt_args = 0;
  82   _stub_id = stub_id;
  83 }
  84 
  85 
  86 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
  87   _name = name;
  88   _must_gc_arguments = must_gc_arguments;
  89 }
  90 
  91 
  92 void StubAssembler::set_frame_size(int size) {
  93   if (_frame_size == no_frame_size) {
  94     _frame_size = size;
  95   }
  96   assert(_frame_size == size, "can't change the frame size");
  97 }
  98 
  99 
 100 void StubAssembler::set_num_rt_args(int args) {
 101   if (_num_rt_args == 0) {
 102     _num_rt_args = args;
 103   }
 104   assert(_num_rt_args == args, "can't change the number of args");
 105 }
 106 
 107 // Implementation of Runtime1
 108 
 109 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids];
 110 const char *Runtime1::_blob_names[] = {
 111   RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME)
 112 };
 113 
 114 #ifndef PRODUCT
 115 // statistics
 116 int Runtime1::_generic_arraycopy_cnt = 0;
 117 int Runtime1::_generic_arraycopystub_cnt = 0;
 118 int Runtime1::_arraycopy_slowcase_cnt = 0;
 119 int Runtime1::_arraycopy_checkcast_cnt = 0;
 120 int Runtime1::_arraycopy_checkcast_attempt_cnt = 0;
 121 int Runtime1::_new_type_array_slowcase_cnt = 0;
 122 int Runtime1::_new_object_array_slowcase_cnt = 0;
 123 int Runtime1::_new_value_array_slowcase_cnt = 0;
 124 int Runtime1::_new_instance_slowcase_cnt = 0;
 125 int Runtime1::_new_multi_array_slowcase_cnt = 0;
 126 int Runtime1::_load_flattened_array_slowcase_cnt = 0;
 127 int Runtime1::_store_flattened_array_slowcase_cnt = 0;
 128 int Runtime1::_substitutability_check_slowcase_cnt = 0;
 129 int Runtime1::_buffer_value_args_slowcase_cnt = 0;
 130 int Runtime1::_buffer_value_args_no_receiver_slowcase_cnt = 0;
 131 int Runtime1::_monitorenter_slowcase_cnt = 0;
 132 int Runtime1::_monitorexit_slowcase_cnt = 0;
 133 int Runtime1::_patch_code_slowcase_cnt = 0;
 134 int Runtime1::_throw_range_check_exception_count = 0;
 135 int Runtime1::_throw_index_exception_count = 0;
 136 int Runtime1::_throw_div0_exception_count = 0;
 137 int Runtime1::_throw_null_pointer_exception_count = 0;
 138 int Runtime1::_throw_class_cast_exception_count = 0;
 139 int Runtime1::_throw_incompatible_class_change_error_count = 0;
 140 int Runtime1::_throw_illegal_monitor_state_exception_count = 0;
 141 int Runtime1::_throw_array_store_exception_count = 0;
 142 int Runtime1::_throw_count = 0;
 143 
 144 static int _byte_arraycopy_stub_cnt = 0;
 145 static int _short_arraycopy_stub_cnt = 0;
 146 static int _int_arraycopy_stub_cnt = 0;
 147 static int _long_arraycopy_stub_cnt = 0;
 148 static int _oop_arraycopy_stub_cnt = 0;
 149 
 150 address Runtime1::arraycopy_count_address(BasicType type) {
 151   switch (type) {
 152   case T_BOOLEAN:
 153   case T_BYTE:   return (address)&_byte_arraycopy_stub_cnt;
 154   case T_CHAR:
 155   case T_SHORT:  return (address)&_short_arraycopy_stub_cnt;
 156   case T_FLOAT:
 157   case T_INT:    return (address)&_int_arraycopy_stub_cnt;
 158   case T_DOUBLE:
 159   case T_LONG:   return (address)&_long_arraycopy_stub_cnt;
 160   case T_ARRAY:
 161   case T_OBJECT: return (address)&_oop_arraycopy_stub_cnt;
 162   default:
 163     ShouldNotReachHere();
 164     return NULL;
 165   }
 166 }
 167 
 168 
 169 #endif
 170 
 171 // Simple helper to see if the caller of a runtime stub which
 172 // entered the VM has been deoptimized
 173 
 174 static bool caller_is_deopted() {
 175   JavaThread* thread = JavaThread::current();
 176   RegisterMap reg_map(thread, false);
 177   frame runtime_frame = thread->last_frame();
 178   frame caller_frame = runtime_frame.sender(&reg_map);
 179   assert(caller_frame.is_compiled_frame(), "must be compiled");
 180   return caller_frame.is_deoptimized_frame();
 181 }
 182 
 183 // Stress deoptimization
 184 static void deopt_caller() {
 185   if ( !caller_is_deopted()) {
 186     JavaThread* thread = JavaThread::current();
 187     RegisterMap reg_map(thread, false);
 188     frame runtime_frame = thread->last_frame();
 189     frame caller_frame = runtime_frame.sender(&reg_map);
 190     Deoptimization::deoptimize_frame(thread, caller_frame.id());
 191     assert(caller_is_deopted(), "Must be deoptimized");
 192   }
 193 }
 194 
 195 class StubIDStubAssemblerCodeGenClosure: public StubAssemblerCodeGenClosure {
 196  private:
 197   Runtime1::StubID _id;
 198  public:
 199   StubIDStubAssemblerCodeGenClosure(Runtime1::StubID id) : _id(id) {}
 200   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 201     return Runtime1::generate_code_for(_id, sasm);
 202   }
 203 };
 204 
 205 CodeBlob* Runtime1::generate_blob(BufferBlob* buffer_blob, int stub_id, const char* name, bool expect_oop_map, StubAssemblerCodeGenClosure* cl) {
 206   ResourceMark rm;
 207   // create code buffer for code storage
 208   CodeBuffer code(buffer_blob);
 209 
 210   OopMapSet* oop_maps;
 211   int frame_size;
 212   bool must_gc_arguments;
 213 
 214   Compilation::setup_code_buffer(&code, 0);
 215 
 216   // create assembler for code generation
 217   StubAssembler* sasm = new StubAssembler(&code, name, stub_id);
 218   // generate code for runtime stub
 219   oop_maps = cl->generate_code(sasm);
 220   assert(oop_maps == NULL || sasm->frame_size() != no_frame_size,
 221          "if stub has an oop map it must have a valid frame size");
 222   assert(!expect_oop_map || oop_maps != NULL, "must have an oopmap");
 223 
 224   // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned)
 225   sasm->align(BytesPerWord);
 226   // make sure all code is in code buffer
 227   sasm->flush();
 228 
 229   frame_size = sasm->frame_size();
 230   must_gc_arguments = sasm->must_gc_arguments();
 231   // create blob - distinguish a few special cases
 232   CodeBlob* blob = RuntimeStub::new_runtime_stub(name,
 233                                                  &code,
 234                                                  CodeOffsets::frame_never_safe,
 235                                                  frame_size,
 236                                                  oop_maps,
 237                                                  must_gc_arguments);
 238   assert(blob != NULL, "blob must exist");
 239   return blob;
 240 }
 241 
 242 void Runtime1::generate_blob_for(BufferBlob* buffer_blob, StubID id) {
 243   assert(0 <= id && id < number_of_ids, "illegal stub id");
 244   bool expect_oop_map = true;
 245 #ifdef ASSERT
 246   // Make sure that stubs that need oopmaps have them
 247   switch (id) {
 248     // These stubs don't need to have an oopmap
 249   case dtrace_object_alloc_id:
 250   case slow_subtype_check_id:
 251   case fpu2long_stub_id:
 252   case unwind_exception_id:
 253   case counter_overflow_id:
 254 #if defined(SPARC) || defined(PPC32)
 255   case handle_exception_nofpu_id:  // Unused on sparc
 256 #endif
 257     expect_oop_map = false;
 258     break;
 259   default:
 260     break;
 261   }
 262 #endif
 263   StubIDStubAssemblerCodeGenClosure cl(id);
 264   CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl);
 265   // install blob
 266   _blobs[id] = blob;
 267 }
 268 
 269 void Runtime1::initialize(BufferBlob* blob) {
 270   // platform-dependent initialization
 271   initialize_pd();
 272   // generate stubs
 273   for (int id = 0; id < number_of_ids; id++) generate_blob_for(blob, (StubID)id);
 274   // printing
 275 #ifndef PRODUCT
 276   if (PrintSimpleStubs) {
 277     ResourceMark rm;
 278     for (int id = 0; id < number_of_ids; id++) {
 279       _blobs[id]->print();
 280       if (_blobs[id]->oop_maps() != NULL) {
 281         _blobs[id]->oop_maps()->print();
 282       }
 283     }
 284   }
 285 #endif
 286   BarrierSetC1* bs = BarrierSet::barrier_set()->barrier_set_c1();
 287   bs->generate_c1_runtime_stubs(blob);
 288 }
 289 
 290 CodeBlob* Runtime1::blob_for(StubID id) {
 291   assert(0 <= id && id < number_of_ids, "illegal stub id");
 292   return _blobs[id];
 293 }
 294 
 295 
 296 const char* Runtime1::name_for(StubID id) {
 297   assert(0 <= id && id < number_of_ids, "illegal stub id");
 298   return _blob_names[id];
 299 }
 300 
 301 const char* Runtime1::name_for_address(address entry) {
 302   for (int id = 0; id < number_of_ids; id++) {
 303     if (entry == entry_for((StubID)id)) return name_for((StubID)id);
 304   }
 305 
 306 #define FUNCTION_CASE(a, f) \
 307   if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f))  return #f
 308 
 309   FUNCTION_CASE(entry, os::javaTimeMillis);
 310   FUNCTION_CASE(entry, os::javaTimeNanos);
 311   FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end);
 312   FUNCTION_CASE(entry, SharedRuntime::d2f);
 313   FUNCTION_CASE(entry, SharedRuntime::d2i);
 314   FUNCTION_CASE(entry, SharedRuntime::d2l);
 315   FUNCTION_CASE(entry, SharedRuntime::dcos);
 316   FUNCTION_CASE(entry, SharedRuntime::dexp);
 317   FUNCTION_CASE(entry, SharedRuntime::dlog);
 318   FUNCTION_CASE(entry, SharedRuntime::dlog10);
 319   FUNCTION_CASE(entry, SharedRuntime::dpow);
 320   FUNCTION_CASE(entry, SharedRuntime::drem);
 321   FUNCTION_CASE(entry, SharedRuntime::dsin);
 322   FUNCTION_CASE(entry, SharedRuntime::dtan);
 323   FUNCTION_CASE(entry, SharedRuntime::f2i);
 324   FUNCTION_CASE(entry, SharedRuntime::f2l);
 325   FUNCTION_CASE(entry, SharedRuntime::frem);
 326   FUNCTION_CASE(entry, SharedRuntime::l2d);
 327   FUNCTION_CASE(entry, SharedRuntime::l2f);
 328   FUNCTION_CASE(entry, SharedRuntime::ldiv);
 329   FUNCTION_CASE(entry, SharedRuntime::lmul);
 330   FUNCTION_CASE(entry, SharedRuntime::lrem);
 331   FUNCTION_CASE(entry, SharedRuntime::lrem);
 332   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry);
 333   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
 334   FUNCTION_CASE(entry, is_instance_of);
 335   FUNCTION_CASE(entry, trace_block_entry);
 336 #ifdef JFR_HAVE_INTRINSICS
 337   FUNCTION_CASE(entry, JFR_TIME_FUNCTION);
 338 #endif
 339   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
 340   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
 341   FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
 342   FUNCTION_CASE(entry, StubRoutines::dexp());
 343   FUNCTION_CASE(entry, StubRoutines::dlog());
 344   FUNCTION_CASE(entry, StubRoutines::dlog10());
 345   FUNCTION_CASE(entry, StubRoutines::dpow());
 346   FUNCTION_CASE(entry, StubRoutines::dsin());
 347   FUNCTION_CASE(entry, StubRoutines::dcos());
 348   FUNCTION_CASE(entry, StubRoutines::dtan());
 349 
 350 #undef FUNCTION_CASE
 351 
 352   // Soft float adds more runtime names.
 353   return pd_name_for_address(entry);
 354 }
 355 
 356 
 357 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, Klass* klass))
 358   NOT_PRODUCT(_new_instance_slowcase_cnt++;)
 359 
 360   assert(klass->is_klass(), "not a class");
 361   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 362   InstanceKlass* h = InstanceKlass::cast(klass);
 363   h->check_valid_for_instantiation(true, CHECK);
 364   // make sure klass is initialized
 365   h->initialize(CHECK);
 366   // allocate instance and return via TLS
 367   oop obj = h->allocate_instance(CHECK);
 368   thread->set_vm_result(obj);
 369 JRT_END
 370 
 371 
 372 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, Klass* klass, jint length))
 373   NOT_PRODUCT(_new_type_array_slowcase_cnt++;)
 374   // Note: no handle for klass needed since they are not used
 375   //       anymore after new_typeArray() and no GC can happen before.
 376   //       (This may have to change if this code changes!)
 377   assert(klass->is_klass(), "not a class");
 378   BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
 379   oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 380   thread->set_vm_result(obj);
 381   // This is pretty rare but this runtime patch is stressful to deoptimization
 382   // if we deoptimize here so force a deopt to stress the path.
 383   if (DeoptimizeALot) {
 384     deopt_caller();
 385   }
 386 
 387 JRT_END
 388 
 389 
 390 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, Klass* array_klass, jint length))
 391   NOT_PRODUCT(_new_object_array_slowcase_cnt++;)
 392 
 393   // Note: no handle for klass needed since they are not used
 394   //       anymore after new_objArray() and no GC can happen before.
 395   //       (This may have to change if this code changes!)
 396   assert(array_klass->is_klass(), "not a class");
 397   Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive
 398   Klass* elem_klass = ArrayKlass::cast(array_klass)->element_klass();
 399   objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 400   thread->set_vm_result(obj);
 401   // This is pretty rare but this runtime patch is stressful to deoptimization
 402   // if we deoptimize here so force a deopt to stress the path.
 403   if (DeoptimizeALot) {
 404     deopt_caller();
 405   }
 406 JRT_END
 407 
 408 
 409 JRT_ENTRY(void, Runtime1::new_value_array(JavaThread* thread, Klass* array_klass, jint length))
 410   NOT_PRODUCT(_new_value_array_slowcase_cnt++;)
 411 
 412   // Note: no handle for klass needed since they are not used
 413   //       anymore after new_objArray() and no GC can happen before.
 414   //       (This may have to change if this code changes!)
 415   assert(array_klass->is_klass(), "not a class");
 416   Handle holder(THREAD, array_klass->klass_holder()); // keep the klass alive
 417   Klass* elem_klass = ArrayKlass::cast(array_klass)->element_klass();
 418   assert(elem_klass->is_value(), "must be");
 419   // Logically creates elements, ensure klass init
 420   elem_klass->initialize(CHECK);
 421   arrayOop obj = oopFactory::new_valueArray(elem_klass, length, CHECK);
 422   thread->set_vm_result(obj);
 423   // This is pretty rare but this runtime patch is stressful to deoptimization
 424   // if we deoptimize here so force a deopt to stress the path.
 425   if (DeoptimizeALot) {
 426     deopt_caller();
 427   }
 428 JRT_END
 429 
 430 
 431 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, Klass* klass, int rank, jint* dims))
 432   NOT_PRODUCT(_new_multi_array_slowcase_cnt++;)
 433 
 434   assert(klass->is_klass(), "not a class");
 435   assert(rank >= 1, "rank must be nonzero");
 436   Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 437   oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 438   thread->set_vm_result(obj);
 439 JRT_END
 440 
 441 
 442 JRT_ENTRY(void, Runtime1::load_flattened_array(JavaThread* thread, valueArrayOopDesc* array, int index))
 443   NOT_PRODUCT(_load_flattened_array_slowcase_cnt++;)
 444   Klass* klass = array->klass();
 445   assert(klass->is_valueArray_klass(), "expected value array oop");
 446   assert(array->length() > 0 && index < array->length(), "already checked");
 447 
 448   ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
 449   ValueKlass* vklass = vaklass->element_klass();
 450 
 451   // We have a non-empty flattened array, so the element type must have been initialized.
 452   assert(vklass->is_initialized(), "must be");
 453   Handle holder(THREAD, vklass->klass_holder()); // keep the vklass alive
 454   valueArrayHandle ha(THREAD, array);
 455   oop obj = vklass->allocate_instance(CHECK);
 456 
 457   void* src = ha()->value_at_addr(index, vaklass->layout_helper());
 458   vklass->value_store(src, vklass->data_for_oop(obj),
 459                       vaklass->element_byte_size(), true, false);
 460   thread->set_vm_result(obj);
 461 JRT_END
 462 
 463 
 464 JRT_ENTRY(void, Runtime1::store_flattened_array(JavaThread* thread, valueArrayOopDesc* array, int index, oopDesc* value))
 465   NOT_PRODUCT(_store_flattened_array_slowcase_cnt++;)
 466   if (value == NULL) {
 467     SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
 468   } else {
 469     Klass* klass = array->klass();
 470     assert(klass->is_valueArray_klass(), "expected value array");
 471     assert(ArrayKlass::cast(klass)->element_klass() == value->klass(), "Store type incorrect");
 472 
 473     ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
 474     ValueKlass* vklass = vaklass->element_klass();
 475     const int lh = vaklass->layout_helper();
 476     vklass->value_store(vklass->data_for_oop(value), array->value_at_addr(index, lh),
 477                         vaklass->element_byte_size(), true, false);
 478   }
 479 JRT_END
 480 
 481 
 482 JRT_ENTRY(int, Runtime1::substitutability_check(JavaThread* thread, oopDesc* left, oopDesc* right))
 483   NOT_PRODUCT(_substitutability_check_slowcase_cnt++;)
 484   JavaCallArguments args;
 485   args.push_oop(Handle(THREAD, left));
 486   args.push_oop(Handle(THREAD, right));
 487   JavaValue result(T_BOOLEAN);
 488   JavaCalls::call_static(&result,
 489                          SystemDictionary::ValueBootstrapMethods_klass(),
 490                          vmSymbols::isSubstitutable_name(),
 491                          vmSymbols::object_object_boolean_signature(),
 492                          &args, CHECK_0);
 493   return result.get_jboolean() ? 1 : 0;
 494 JRT_END
 495 
 496 
 497 extern "C" void ps();
 498 
 499 void Runtime1::buffer_value_args_impl(JavaThread* thread, Method* m, bool allocate_receiver) {
 500   Thread* THREAD = thread;
 501   methodHandle method(m); // We are inside the verified_entry or verified_value_ro_entry of this method.
 502   oop obj = SharedRuntime::allocate_value_types_impl(thread, method, allocate_receiver, CHECK);
 503   thread->set_vm_result(obj);
 504 }
 505 
 506 JRT_ENTRY(void, Runtime1::buffer_value_args(JavaThread* thread, Method* method))
 507   NOT_PRODUCT(_buffer_value_args_slowcase_cnt++;)
 508   buffer_value_args_impl(thread, method, true);
 509 JRT_END
 510 
 511 JRT_ENTRY(void, Runtime1::buffer_value_args_no_receiver(JavaThread* thread, Method* method))
 512   NOT_PRODUCT(_buffer_value_args_no_receiver_slowcase_cnt++;)
 513   buffer_value_args_impl(thread, method, false);
 514 JRT_END
 515 
 516 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id))
 517   tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id);
 518 JRT_END
 519 
 520 
 521 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread, oopDesc* obj))
 522   ResourceMark rm(thread);
 523   const char* klass_name = obj->klass()->external_name();
 524   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayStoreException(), klass_name);
 525 JRT_END
 526 
 527 
 528 // counter_overflow() is called from within C1-compiled methods. The enclosing method is the method
 529 // associated with the top activation record. The inlinee (that is possibly included in the enclosing
 530 // method) method oop is passed as an argument. In order to do that it is embedded in the code as
 531 // a constant.
 532 static nmethod* counter_overflow_helper(JavaThread* THREAD, int branch_bci, Method* m) {
 533   nmethod* osr_nm = NULL;
 534   methodHandle method(THREAD, m);
 535 
 536   RegisterMap map(THREAD, false);
 537   frame fr =  THREAD->last_frame().sender(&map);
 538   nmethod* nm = (nmethod*) fr.cb();
 539   assert(nm!= NULL && nm->is_nmethod(), "Sanity check");
 540   methodHandle enclosing_method(THREAD, nm->method());
 541 
 542   CompLevel level = (CompLevel)nm->comp_level();
 543   int bci = InvocationEntryBci;
 544   if (branch_bci != InvocationEntryBci) {
 545     // Compute destination bci
 546     address pc = method()->code_base() + branch_bci;
 547     Bytecodes::Code branch = Bytecodes::code_at(method(), pc);
 548     int offset = 0;
 549     switch (branch) {
 550       case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
 551       case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
 552       case Bytecodes::_if_icmple: case Bytecodes::_ifle:
 553       case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
 554       case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
 555       case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
 556       case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
 557         offset = (int16_t)Bytes::get_Java_u2(pc + 1);
 558         break;
 559       case Bytecodes::_goto_w:
 560         offset = Bytes::get_Java_u4(pc + 1);
 561         break;
 562       default: ;
 563     }
 564     bci = branch_bci + offset;
 565   }
 566   assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
 567   osr_nm = CompilationPolicy::policy()->event(enclosing_method, method, branch_bci, bci, level, nm, THREAD);
 568   assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
 569   return osr_nm;
 570 }
 571 
 572 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* thread, int bci, Method* method))
 573   nmethod* osr_nm;
 574   JRT_BLOCK
 575     osr_nm = counter_overflow_helper(thread, bci, method);
 576     if (osr_nm != NULL) {
 577       RegisterMap map(thread, false);
 578       frame fr =  thread->last_frame().sender(&map);
 579       Deoptimization::deoptimize_frame(thread, fr.id());
 580     }
 581   JRT_BLOCK_END
 582   return NULL;
 583 JRT_END
 584 
 585 extern void vm_exit(int code);
 586 
 587 // Enter this method from compiled code handler below. This is where we transition
 588 // to VM mode. This is done as a helper routine so that the method called directly
 589 // from compiled code does not have to transition to VM. This allows the entry
 590 // method to see if the nmethod that we have just looked up a handler for has
 591 // been deoptimized while we were in the vm. This simplifies the assembly code
 592 // cpu directories.
 593 //
 594 // We are entering here from exception stub (via the entry method below)
 595 // If there is a compiled exception handler in this method, we will continue there;
 596 // otherwise we will unwind the stack and continue at the caller of top frame method
 597 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 598 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 599 // check to see if the handler we are going to return is now in a nmethod that has
 600 // been deoptimized. If that is the case we return the deopt blob
 601 // unpack_with_exception entry instead. This makes life for the exception blob easier
 602 // because making that same check and diverting is painful from assembly language.
 603 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm))
 604   // Reset method handle flag.
 605   thread->set_is_method_handle_return(false);
 606 
 607   Handle exception(thread, ex);
 608   nm = CodeCache::find_nmethod(pc);
 609   assert(nm != NULL, "this is not an nmethod");
 610   // Adjust the pc as needed/
 611   if (nm->is_deopt_pc(pc)) {
 612     RegisterMap map(thread, false);
 613     frame exception_frame = thread->last_frame().sender(&map);
 614     // if the frame isn't deopted then pc must not correspond to the caller of last_frame
 615     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
 616     pc = exception_frame.pc();
 617   }
 618 #ifdef ASSERT
 619   assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
 620   // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
 621   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
 622     if (ExitVMOnVerifyError) vm_exit(-1);
 623     ShouldNotReachHere();
 624   }
 625 #endif
 626 
 627   // Check the stack guard pages and reenable them if necessary and there is
 628   // enough space on the stack to do so.  Use fast exceptions only if the guard
 629   // pages are enabled.
 630   bool guard_pages_enabled = thread->stack_guards_enabled();
 631   if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
 632 
 633   if (JvmtiExport::can_post_on_exceptions()) {
 634     // To ensure correct notification of exception catches and throws
 635     // we have to deoptimize here.  If we attempted to notify the
 636     // catches and throws during this exception lookup it's possible
 637     // we could deoptimize on the way out of the VM and end back in
 638     // the interpreter at the throw site.  This would result in double
 639     // notifications since the interpreter would also notify about
 640     // these same catches and throws as it unwound the frame.
 641 
 642     RegisterMap reg_map(thread);
 643     frame stub_frame = thread->last_frame();
 644     frame caller_frame = stub_frame.sender(&reg_map);
 645 
 646     // We don't really want to deoptimize the nmethod itself since we
 647     // can actually continue in the exception handler ourselves but I
 648     // don't see an easy way to have the desired effect.
 649     Deoptimization::deoptimize_frame(thread, caller_frame.id());
 650     assert(caller_is_deopted(), "Must be deoptimized");
 651 
 652     return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 653   }
 654 
 655   // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
 656   if (guard_pages_enabled) {
 657     address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
 658     if (fast_continuation != NULL) {
 659       // Set flag if return address is a method handle call site.
 660       thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
 661       return fast_continuation;
 662     }
 663   }
 664 
 665   // If the stack guard pages are enabled, check whether there is a handler in
 666   // the current method.  Otherwise (guard pages disabled), force an unwind and
 667   // skip the exception cache update (i.e., just leave continuation==NULL).
 668   address continuation = NULL;
 669   if (guard_pages_enabled) {
 670 
 671     // New exception handling mechanism can support inlined methods
 672     // with exception handlers since the mappings are from PC to PC
 673 
 674     // debugging support
 675     // tracing
 676     if (log_is_enabled(Info, exceptions)) {
 677       ResourceMark rm;
 678       stringStream tempst;
 679       assert(nm->method() != NULL, "Unexpected NULL method()");
 680       tempst.print("compiled method <%s>\n"
 681                    " at PC" INTPTR_FORMAT " for thread " INTPTR_FORMAT,
 682                    nm->method()->print_value_string(), p2i(pc), p2i(thread));
 683       Exceptions::log_exception(exception, tempst.as_string());
 684     }
 685     // for AbortVMOnException flag
 686     Exceptions::debug_check_abort(exception);
 687 
 688     // Clear out the exception oop and pc since looking up an
 689     // exception handler can cause class loading, which might throw an
 690     // exception and those fields are expected to be clear during
 691     // normal bytecode execution.
 692     thread->clear_exception_oop_and_pc();
 693 
 694     bool recursive_exception = false;
 695     continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false, recursive_exception);
 696     // If an exception was thrown during exception dispatch, the exception oop may have changed
 697     thread->set_exception_oop(exception());
 698     thread->set_exception_pc(pc);
 699 
 700     // the exception cache is used only by non-implicit exceptions
 701     // Update the exception cache only when there didn't happen
 702     // another exception during the computation of the compiled
 703     // exception handler. Checking for exception oop equality is not
 704     // sufficient because some exceptions are pre-allocated and reused.
 705     if (continuation != NULL && !recursive_exception) {
 706       nm->add_handler_for_exception_and_pc(exception, pc, continuation);
 707     }
 708   }
 709 
 710   thread->set_vm_result(exception());
 711   // Set flag if return address is a method handle call site.
 712   thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
 713 
 714   if (log_is_enabled(Info, exceptions)) {
 715     ResourceMark rm;
 716     log_info(exceptions)("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT
 717                          " for exception thrown at PC " PTR_FORMAT,
 718                          p2i(thread), p2i(continuation), p2i(pc));
 719   }
 720 
 721   return continuation;
 722 JRT_END
 723 
 724 // Enter this method from compiled code only if there is a Java exception handler
 725 // in the method handling the exception.
 726 // We are entering here from exception stub. We don't do a normal VM transition here.
 727 // We do it in a helper. This is so we can check to see if the nmethod we have just
 728 // searched for an exception handler has been deoptimized in the meantime.
 729 address Runtime1::exception_handler_for_pc(JavaThread* thread) {
 730   oop exception = thread->exception_oop();
 731   address pc = thread->exception_pc();
 732   // Still in Java mode
 733   DEBUG_ONLY(ResetNoHandleMark rnhm);
 734   nmethod* nm = NULL;
 735   address continuation = NULL;
 736   {
 737     // Enter VM mode by calling the helper
 738     ResetNoHandleMark rnhm;
 739     continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
 740   }
 741   // Back in JAVA, use no oops DON'T safepoint
 742 
 743   // Now check to see if the nmethod we were called from is now deoptimized.
 744   // If so we must return to the deopt blob and deoptimize the nmethod
 745   if (nm != NULL && caller_is_deopted()) {
 746     continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 747   }
 748 
 749   assert(continuation != NULL, "no handler found");
 750   return continuation;
 751 }
 752 
 753 
 754 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index, arrayOopDesc* a))
 755   NOT_PRODUCT(_throw_range_check_exception_count++;)
 756   const int len = 35;
 757   assert(len < strlen("Index %d out of bounds for length %d"), "Must allocate more space for message.");
 758   char message[2 * jintAsStringSize + len];
 759   sprintf(message, "Index %d out of bounds for length %d", index, a->length());
 760   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message);
 761 JRT_END
 762 
 763 
 764 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index))
 765   NOT_PRODUCT(_throw_index_exception_count++;)
 766   char message[16];
 767   sprintf(message, "%d", index);
 768   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message);
 769 JRT_END
 770 
 771 
 772 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread))
 773   NOT_PRODUCT(_throw_div0_exception_count++;)
 774   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
 775 JRT_END
 776 
 777 
 778 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread))
 779   NOT_PRODUCT(_throw_null_pointer_exception_count++;)
 780   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
 781 JRT_END
 782 
 783 
 784 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object))
 785   NOT_PRODUCT(_throw_class_cast_exception_count++;)
 786   ResourceMark rm(thread);
 787   char* message = SharedRuntime::generate_class_cast_message(
 788     thread, object->klass());
 789   SharedRuntime::throw_and_post_jvmti_exception(
 790     thread, vmSymbols::java_lang_ClassCastException(), message);
 791 JRT_END
 792 
 793 
 794 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread))
 795   NOT_PRODUCT(_throw_incompatible_class_change_error_count++;)
 796   ResourceMark rm(thread);
 797   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError());
 798 JRT_END
 799 
 800 
 801 JRT_ENTRY(void, Runtime1::throw_illegal_monitor_state_exception(JavaThread* thread))
 802   NOT_PRODUCT(_throw_illegal_monitor_state_exception_count++;)
 803   ResourceMark rm(thread);
 804   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IllegalMonitorStateException());
 805 JRT_END
 806 
 807 
 808 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock))
 809   NOT_PRODUCT(_monitorenter_slowcase_cnt++;)
 810   if (PrintBiasedLockingStatistics) {
 811     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
 812   }
 813   Handle h_obj(thread, obj);
 814   if (UseBiasedLocking) {
 815     // Retry fast entry if bias is revoked to avoid unnecessary inflation
 816     ObjectSynchronizer::fast_enter(h_obj, lock->lock(), true, CHECK);
 817   } else {
 818     if (UseFastLocking) {
 819       // When using fast locking, the compiled code has already tried the fast case
 820       assert(obj == lock->obj(), "must match");
 821       ObjectSynchronizer::slow_enter(h_obj, lock->lock(), THREAD);
 822     } else {
 823       lock->set_obj(obj);
 824       ObjectSynchronizer::fast_enter(h_obj, lock->lock(), false, THREAD);
 825     }
 826   }
 827 JRT_END
 828 
 829 
 830 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock))
 831   NOT_PRODUCT(_monitorexit_slowcase_cnt++;)
 832   assert(thread == JavaThread::current(), "threads must correspond");
 833   assert(thread->last_Java_sp(), "last_Java_sp must be set");
 834   // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
 835   EXCEPTION_MARK;
 836 
 837   oop obj = lock->obj();
 838   assert(oopDesc::is_oop(obj), "must be NULL or an object");
 839   if (UseFastLocking) {
 840     // When using fast locking, the compiled code has already tried the fast case
 841     ObjectSynchronizer::slow_exit(obj, lock->lock(), THREAD);
 842   } else {
 843     ObjectSynchronizer::fast_exit(obj, lock->lock(), THREAD);
 844   }
 845 JRT_END
 846 
 847 // Cf. OptoRuntime::deoptimize_caller_frame
 848 JRT_ENTRY(void, Runtime1::deoptimize(JavaThread* thread, jint trap_request))
 849   // Called from within the owner thread, so no need for safepoint
 850   RegisterMap reg_map(thread, false);
 851   frame stub_frame = thread->last_frame();
 852   assert(stub_frame.is_runtime_frame(), "Sanity check");
 853   frame caller_frame = stub_frame.sender(&reg_map);
 854   nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
 855   assert(nm != NULL, "Sanity check");
 856   methodHandle method(thread, nm->method());
 857   assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
 858   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
 859   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
 860 
 861   if (action == Deoptimization::Action_make_not_entrant) {
 862     if (nm->make_not_entrant()) {
 863       if (reason == Deoptimization::Reason_tenured) {
 864         MethodData* trap_mdo = Deoptimization::get_method_data(thread, method, true /*create_if_missing*/);
 865         if (trap_mdo != NULL) {
 866           trap_mdo->inc_tenure_traps();
 867         }
 868       }
 869     }
 870   }
 871 
 872   // Deoptimize the caller frame.
 873   Deoptimization::deoptimize_frame(thread, caller_frame.id());
 874   // Return to the now deoptimized frame.
 875 JRT_END
 876 
 877 
 878 #ifndef DEOPTIMIZE_WHEN_PATCHING
 879 
 880 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
 881   Bytecode_field field_access(caller, bci);
 882   // This can be static or non-static field access
 883   Bytecodes::Code code       = field_access.code();
 884 
 885   // We must load class, initialize class and resolve the field
 886   fieldDescriptor result; // initialize class if needed
 887   constantPoolHandle constants(THREAD, caller->constants());
 888   LinkResolver::resolve_field_access(result, constants, field_access.index(), caller, Bytecodes::java_code(code), CHECK_NULL);
 889   return result.field_holder();
 890 }
 891 
 892 
 893 //
 894 // This routine patches sites where a class wasn't loaded or
 895 // initialized at the time the code was generated.  It handles
 896 // references to classes, fields and forcing of initialization.  Most
 897 // of the cases are straightforward and involving simply forcing
 898 // resolution of a class, rewriting the instruction stream with the
 899 // needed constant and replacing the call in this function with the
 900 // patched code.  The case for static field is more complicated since
 901 // the thread which is in the process of initializing a class can
 902 // access it's static fields but other threads can't so the code
 903 // either has to deoptimize when this case is detected or execute a
 904 // check that the current thread is the initializing thread.  The
 905 // current
 906 //
 907 // Patches basically look like this:
 908 //
 909 //
 910 // patch_site: jmp patch stub     ;; will be patched
 911 // continue:   ...
 912 //             ...
 913 //             ...
 914 //             ...
 915 //
 916 // They have a stub which looks like this:
 917 //
 918 //             ;; patch body
 919 //             movl <const>, reg           (for class constants)
 920 //        <or> movl [reg1 + <const>], reg  (for field offsets)
 921 //        <or> movl reg, [reg1 + <const>]  (for field offsets)
 922 //             <being_init offset> <bytes to copy> <bytes to skip>
 923 // patch_stub: call Runtime1::patch_code (through a runtime stub)
 924 //             jmp patch_site
 925 //
 926 //
 927 // A normal patch is done by rewriting the patch body, usually a move,
 928 // and then copying it into place over top of the jmp instruction
 929 // being careful to flush caches and doing it in an MP-safe way.  The
 930 // constants following the patch body are used to find various pieces
 931 // of the patch relative to the call site for Runtime1::patch_code.
 932 // The case for getstatic and putstatic is more complicated because
 933 // getstatic and putstatic have special semantics when executing while
 934 // the class is being initialized.  getstatic/putstatic on a class
 935 // which is being_initialized may be executed by the initializing
 936 // thread but other threads have to block when they execute it.  This
 937 // is accomplished in compiled code by executing a test of the current
 938 // thread against the initializing thread of the class.  It's emitted
 939 // as boilerplate in their stub which allows the patched code to be
 940 // executed before it's copied back into the main body of the nmethod.
 941 //
 942 // being_init: get_thread(<tmp reg>
 943 //             cmpl [reg1 + <init_thread_offset>], <tmp reg>
 944 //             jne patch_stub
 945 //             movl [reg1 + <const>], reg  (for field offsets)  <or>
 946 //             movl reg, [reg1 + <const>]  (for field offsets)
 947 //             jmp continue
 948 //             <being_init offset> <bytes to copy> <bytes to skip>
 949 // patch_stub: jmp Runtim1::patch_code (through a runtime stub)
 950 //             jmp patch_site
 951 //
 952 // If the class is being initialized the patch body is rewritten and
 953 // the patch site is rewritten to jump to being_init, instead of
 954 // patch_stub.  Whenever this code is executed it checks the current
 955 // thread against the intializing thread so other threads will enter
 956 // the runtime and end up blocked waiting the class to finish
 957 // initializing inside the calls to resolve_field below.  The
 958 // initializing class will continue on it's way.  Once the class is
 959 // fully_initialized, the intializing_thread of the class becomes
 960 // NULL, so the next thread to execute this code will fail the test,
 961 // call into patch_code and complete the patching process by copying
 962 // the patch body back into the main part of the nmethod and resume
 963 // executing.
 964 
 965 // NB:
 966 //
 967 // Patchable instruction sequences inherently exhibit race conditions,
 968 // where thread A is patching an instruction at the same time thread B
 969 // is executing it.  The algorithms we use ensure that any observation
 970 // that B can make on any intermediate states during A's patching will
 971 // always end up with a correct outcome.  This is easiest if there are
 972 // few or no intermediate states.  (Some inline caches have two
 973 // related instructions that must be patched in tandem.  For those,
 974 // intermediate states seem to be unavoidable, but we will get the
 975 // right answer from all possible observation orders.)
 976 //
 977 // When patching the entry instruction at the head of a method, or a
 978 // linkable call instruction inside of a method, we try very hard to
 979 // use a patch sequence which executes as a single memory transaction.
 980 // This means, in practice, that when thread A patches an instruction,
 981 // it should patch a 32-bit or 64-bit word that somehow overlaps the
 982 // instruction or is contained in it.  We believe that memory hardware
 983 // will never break up such a word write, if it is naturally aligned
 984 // for the word being written.  We also know that some CPUs work very
 985 // hard to create atomic updates even of naturally unaligned words,
 986 // but we don't want to bet the farm on this always working.
 987 //
 988 // Therefore, if there is any chance of a race condition, we try to
 989 // patch only naturally aligned words, as single, full-word writes.
 990 
 991 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
 992   NOT_PRODUCT(_patch_code_slowcase_cnt++;)
 993 
 994   ResourceMark rm(thread);
 995   RegisterMap reg_map(thread, false);
 996   frame runtime_frame = thread->last_frame();
 997   frame caller_frame = runtime_frame.sender(&reg_map);
 998 
 999   // last java frame on stack
1000   vframeStream vfst(thread, true);
1001   assert(!vfst.at_end(), "Java frame must exist");
1002 
1003   methodHandle caller_method(THREAD, vfst.method());
1004   // Note that caller_method->code() may not be same as caller_code because of OSR's
1005   // Note also that in the presence of inlining it is not guaranteed
1006   // that caller_method() == caller_code->method()
1007 
1008   int bci = vfst.bci();
1009   Bytecodes::Code code = caller_method()->java_code_at(bci);
1010 
1011   // this is used by assertions in the access_field_patching_id
1012   BasicType patch_field_type = T_ILLEGAL;
1013   bool deoptimize_for_volatile = false;
1014   bool deoptimize_for_atomic = false;
1015   int patch_field_offset = -1;
1016   Klass* init_klass = NULL; // klass needed by load_klass_patching code
1017   Klass* load_klass = NULL; // klass needed by load_klass_patching code
1018   Handle mirror(THREAD, NULL);                    // oop needed by load_mirror_patching code
1019   Handle appendix(THREAD, NULL);                  // oop needed by appendix_patching code
1020   bool load_klass_or_mirror_patch_id =
1021     (stub_id == Runtime1::load_klass_patching_id || stub_id == Runtime1::load_mirror_patching_id);
1022 
1023   if (stub_id == Runtime1::access_field_patching_id) {
1024 
1025     Bytecode_field field_access(caller_method, bci);
1026     fieldDescriptor result; // initialize class if needed
1027     Bytecodes::Code code = field_access.code();
1028     constantPoolHandle constants(THREAD, caller_method->constants());
1029     LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method, Bytecodes::java_code(code), CHECK);
1030     patch_field_offset = result.offset();
1031 
1032     // If we're patching a field which is volatile then at compile it
1033     // must not have been know to be volatile, so the generated code
1034     // isn't correct for a volatile reference.  The nmethod has to be
1035     // deoptimized so that the code can be regenerated correctly.
1036     // This check is only needed for access_field_patching since this
1037     // is the path for patching field offsets.  load_klass is only
1038     // used for patching references to oops which don't need special
1039     // handling in the volatile case.
1040 
1041     deoptimize_for_volatile = result.access_flags().is_volatile();
1042 
1043     // If we are patching a field which should be atomic, then
1044     // the generated code is not correct either, force deoptimizing.
1045     // We need to only cover T_LONG and T_DOUBLE fields, as we can
1046     // break access atomicity only for them.
1047 
1048     // Strictly speaking, the deoptimization on 64-bit platforms
1049     // is unnecessary, and T_LONG stores on 32-bit platforms need
1050     // to be handled by special patching code when AlwaysAtomicAccesses
1051     // becomes product feature. At this point, we are still going
1052     // for the deoptimization for consistency against volatile
1053     // accesses.
1054 
1055     patch_field_type = result.field_type();
1056     deoptimize_for_atomic = (AlwaysAtomicAccesses && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG));
1057 
1058   } else if (load_klass_or_mirror_patch_id) {
1059     Klass* k = NULL;
1060     switch (code) {
1061       case Bytecodes::_putstatic:
1062       case Bytecodes::_getstatic:
1063         { Klass* klass = resolve_field_return_klass(caller_method, bci, CHECK);
1064           init_klass = klass;
1065           mirror = Handle(THREAD, klass->java_mirror());
1066         }
1067         break;
1068       case Bytecodes::_new:
1069         { Bytecode_new bnew(caller_method(), caller_method->bcp_from(bci));
1070           k = caller_method->constants()->klass_at(bnew.index(), CHECK);
1071         }
1072         break;
1073       case Bytecodes::_defaultvalue:
1074         { Bytecode_defaultvalue bdefaultvalue(caller_method(), caller_method->bcp_from(bci));
1075           k = caller_method->constants()->klass_at(bdefaultvalue.index(), CHECK);
1076         }
1077         break;
1078       case Bytecodes::_multianewarray:
1079         { Bytecode_multianewarray mna(caller_method(), caller_method->bcp_from(bci));
1080           k = caller_method->constants()->klass_at(mna.index(), CHECK);
1081           if (k->name()->is_Q_array_signature()) {
1082             // Logically creates elements, ensure klass init
1083             k->initialize(CHECK);
1084           }
1085         }
1086         break;
1087       case Bytecodes::_instanceof:
1088         { Bytecode_instanceof io(caller_method(), caller_method->bcp_from(bci));
1089           k = caller_method->constants()->klass_at(io.index(), CHECK);
1090         }
1091         break;
1092       case Bytecodes::_checkcast:
1093         { Bytecode_checkcast cc(caller_method(), caller_method->bcp_from(bci));
1094           k = caller_method->constants()->klass_at(cc.index(), CHECK);
1095         }
1096         break;
1097       case Bytecodes::_anewarray:
1098         { Bytecode_anewarray anew(caller_method(), caller_method->bcp_from(bci));
1099           Klass* ek = caller_method->constants()->klass_at(anew.index(), CHECK);
1100           if (ek->is_value() && caller_method->constants()->klass_at_noresolve(anew.index())->is_Q_signature()) {
1101             k = ek->array_klass(ArrayStorageProperties::flattened_and_null_free, 1, CHECK);
1102             assert(ArrayKlass::cast(k)->storage_properties().is_null_free(), "Expect a null-free array class here");
1103           } else {
1104             k = ek->array_klass(CHECK);
1105           }
1106         }
1107         break;
1108       case Bytecodes::_ldc:
1109       case Bytecodes::_ldc_w:
1110         {
1111           Bytecode_loadconstant cc(caller_method, bci);
1112           oop m = cc.resolve_constant(CHECK);
1113           mirror = Handle(THREAD, m);
1114         }
1115         break;
1116       default: fatal("unexpected bytecode for load_klass_or_mirror_patch_id");
1117     }
1118     load_klass = k;
1119   } else if (stub_id == load_appendix_patching_id) {
1120     Bytecode_invoke bytecode(caller_method, bci);
1121     Bytecodes::Code bc = bytecode.invoke_code();
1122 
1123     CallInfo info;
1124     constantPoolHandle pool(thread, caller_method->constants());
1125     int index = bytecode.index();
1126     LinkResolver::resolve_invoke(info, Handle(), pool, index, bc, CHECK);
1127     switch (bc) {
1128       case Bytecodes::_invokehandle: {
1129         int cache_index = ConstantPool::decode_cpcache_index(index, true);
1130         assert(cache_index >= 0 && cache_index < pool->cache()->length(), "unexpected cache index");
1131         ConstantPoolCacheEntry* cpce = pool->cache()->entry_at(cache_index);
1132         cpce->set_method_handle(pool, info);
1133         appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry
1134         break;
1135       }
1136       case Bytecodes::_invokedynamic: {
1137         ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1138         cpce->set_dynamic_call(pool, info);
1139         appendix = Handle(THREAD, cpce->appendix_if_resolved(pool)); // just in case somebody already resolved the entry
1140         break;
1141       }
1142       default: fatal("unexpected bytecode for load_appendix_patching_id");
1143     }
1144   } else {
1145     ShouldNotReachHere();
1146   }
1147 
1148   if (deoptimize_for_volatile || deoptimize_for_atomic) {
1149     // At compile time we assumed the field wasn't volatile/atomic but after
1150     // loading it turns out it was volatile/atomic so we have to throw the
1151     // compiled code out and let it be regenerated.
1152     if (TracePatching) {
1153       if (deoptimize_for_volatile) {
1154         tty->print_cr("Deoptimizing for patching volatile field reference");
1155       }
1156       if (deoptimize_for_atomic) {
1157         tty->print_cr("Deoptimizing for patching atomic field reference");
1158       }
1159     }
1160 
1161     // It's possible the nmethod was invalidated in the last
1162     // safepoint, but if it's still alive then make it not_entrant.
1163     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1164     if (nm != NULL) {
1165       nm->make_not_entrant();
1166     }
1167 
1168     Deoptimization::deoptimize_frame(thread, caller_frame.id());
1169 
1170     // Return to the now deoptimized frame.
1171   }
1172 
1173   // Now copy code back
1174 
1175   {
1176     MutexLocker ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag);
1177     //
1178     // Deoptimization may have happened while we waited for the lock.
1179     // In that case we don't bother to do any patching we just return
1180     // and let the deopt happen
1181     if (!caller_is_deopted()) {
1182       NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc());
1183       address instr_pc = jump->jump_destination();
1184       NativeInstruction* ni = nativeInstruction_at(instr_pc);
1185       if (ni->is_jump() ) {
1186         // the jump has not been patched yet
1187         // The jump destination is slow case and therefore not part of the stubs
1188         // (stubs are only for StaticCalls)
1189 
1190         // format of buffer
1191         //    ....
1192         //    instr byte 0     <-- copy_buff
1193         //    instr byte 1
1194         //    ..
1195         //    instr byte n-1
1196         //      n
1197         //    ....             <-- call destination
1198 
1199         address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset();
1200         unsigned char* byte_count = (unsigned char*) (stub_location - 1);
1201         unsigned char* byte_skip = (unsigned char*) (stub_location - 2);
1202         unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3);
1203         address copy_buff = stub_location - *byte_skip - *byte_count;
1204         address being_initialized_entry = stub_location - *being_initialized_entry_offset;
1205         if (TracePatching) {
1206           ttyLocker ttyl;
1207           tty->print_cr(" Patching %s at bci %d at address " INTPTR_FORMAT "  (%s)", Bytecodes::name(code), bci,
1208                         p2i(instr_pc), (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass");
1209           nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc());
1210           assert(caller_code != NULL, "nmethod not found");
1211 
1212           // NOTE we use pc() not original_pc() because we already know they are
1213           // identical otherwise we'd have never entered this block of code
1214 
1215           const ImmutableOopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc());
1216           assert(map != NULL, "null check");
1217           map->print();
1218           tty->cr();
1219 
1220           Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1221         }
1222         // depending on the code below, do_patch says whether to copy the patch body back into the nmethod
1223         bool do_patch = true;
1224         if (stub_id == Runtime1::access_field_patching_id) {
1225           // The offset may not be correct if the class was not loaded at code generation time.
1226           // Set it now.
1227           NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff);
1228           assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type");
1229           assert(patch_field_offset >= 0, "illegal offset");
1230           n_move->add_offset_in_bytes(patch_field_offset);
1231         } else if (load_klass_or_mirror_patch_id) {
1232           // If a getstatic or putstatic is referencing a klass which
1233           // isn't fully initialized, the patch body isn't copied into
1234           // place until initialization is complete.  In this case the
1235           // patch site is setup so that any threads besides the
1236           // initializing thread are forced to come into the VM and
1237           // block.
1238           do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) ||
1239                      InstanceKlass::cast(init_klass)->is_initialized();
1240           NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc);
1241           if (jump->jump_destination() == being_initialized_entry) {
1242             assert(do_patch == true, "initialization must be complete at this point");
1243           } else {
1244             // patch the instruction <move reg, klass>
1245             NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
1246 
1247             assert(n_copy->data() == 0 ||
1248                    n_copy->data() == (intptr_t)Universe::non_oop_word(),
1249                    "illegal init value");
1250             if (stub_id == Runtime1::load_klass_patching_id) {
1251               assert(load_klass != NULL, "klass not set");
1252               n_copy->set_data((intx) (load_klass));
1253             } else {
1254               assert(mirror() != NULL, "klass not set");
1255               // Don't need a G1 pre-barrier here since we assert above that data isn't an oop.
1256               n_copy->set_data(cast_from_oop<intx>(mirror()));
1257             }
1258 
1259             if (TracePatching) {
1260               Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1261             }
1262           }
1263         } else if (stub_id == Runtime1::load_appendix_patching_id) {
1264           NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
1265           assert(n_copy->data() == 0 ||
1266                  n_copy->data() == (intptr_t)Universe::non_oop_word(),
1267                  "illegal init value");
1268           n_copy->set_data(cast_from_oop<intx>(appendix()));
1269 
1270           if (TracePatching) {
1271             Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
1272           }
1273         } else {
1274           ShouldNotReachHere();
1275         }
1276 
1277 #if defined(SPARC) || defined(PPC32)
1278         if (load_klass_or_mirror_patch_id ||
1279             stub_id == Runtime1::load_appendix_patching_id) {
1280           // Update the location in the nmethod with the proper
1281           // metadata.  When the code was generated, a NULL was stuffed
1282           // in the metadata table and that table needs to be update to
1283           // have the right value.  On intel the value is kept
1284           // directly in the instruction instead of in the metadata
1285           // table, so set_data above effectively updated the value.
1286           nmethod* nm = CodeCache::find_nmethod(instr_pc);
1287           assert(nm != NULL, "invalid nmethod_pc");
1288           RelocIterator mds(nm, copy_buff, copy_buff + 1);
1289           bool found = false;
1290           while (mds.next() && !found) {
1291             if (mds.type() == relocInfo::oop_type) {
1292               assert(stub_id == Runtime1::load_mirror_patching_id ||
1293                      stub_id == Runtime1::load_appendix_patching_id, "wrong stub id");
1294               oop_Relocation* r = mds.oop_reloc();
1295               oop* oop_adr = r->oop_addr();
1296               *oop_adr = stub_id == Runtime1::load_mirror_patching_id ? mirror() : appendix();
1297               r->fix_oop_relocation();
1298               found = true;
1299             } else if (mds.type() == relocInfo::metadata_type) {
1300               assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id");
1301               metadata_Relocation* r = mds.metadata_reloc();
1302               Metadata** metadata_adr = r->metadata_addr();
1303               *metadata_adr = load_klass;
1304               r->fix_metadata_relocation();
1305               found = true;
1306             }
1307           }
1308           assert(found, "the metadata must exist!");
1309         }
1310 #endif
1311         if (do_patch) {
1312           // replace instructions
1313           // first replace the tail, then the call
1314 #ifdef ARM
1315           if((load_klass_or_mirror_patch_id ||
1316               stub_id == Runtime1::load_appendix_patching_id) &&
1317               nativeMovConstReg_at(copy_buff)->is_pc_relative()) {
1318             nmethod* nm = CodeCache::find_nmethod(instr_pc);
1319             address addr = NULL;
1320             assert(nm != NULL, "invalid nmethod_pc");
1321             RelocIterator mds(nm, copy_buff, copy_buff + 1);
1322             while (mds.next()) {
1323               if (mds.type() == relocInfo::oop_type) {
1324                 assert(stub_id == Runtime1::load_mirror_patching_id ||
1325                        stub_id == Runtime1::load_appendix_patching_id, "wrong stub id");
1326                 oop_Relocation* r = mds.oop_reloc();
1327                 addr = (address)r->oop_addr();
1328                 break;
1329               } else if (mds.type() == relocInfo::metadata_type) {
1330                 assert(stub_id == Runtime1::load_klass_patching_id, "wrong stub id");
1331                 metadata_Relocation* r = mds.metadata_reloc();
1332                 addr = (address)r->metadata_addr();
1333                 break;
1334               }
1335             }
1336             assert(addr != NULL, "metadata relocation must exist");
1337             copy_buff -= *byte_count;
1338             NativeMovConstReg* n_copy2 = nativeMovConstReg_at(copy_buff);
1339             n_copy2->set_pc_relative_offset(addr, instr_pc);
1340           }
1341 #endif
1342 
1343           for (int i = NativeGeneralJump::instruction_size; i < *byte_count; i++) {
1344             address ptr = copy_buff + i;
1345             int a_byte = (*ptr) & 0xFF;
1346             address dst = instr_pc + i;
1347             *(unsigned char*)dst = (unsigned char) a_byte;
1348           }
1349           ICache::invalidate_range(instr_pc, *byte_count);
1350           NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff);
1351 
1352           if (load_klass_or_mirror_patch_id ||
1353               stub_id == Runtime1::load_appendix_patching_id) {
1354             relocInfo::relocType rtype =
1355               (stub_id == Runtime1::load_klass_patching_id) ?
1356                                    relocInfo::metadata_type :
1357                                    relocInfo::oop_type;
1358             // update relocInfo to metadata
1359             nmethod* nm = CodeCache::find_nmethod(instr_pc);
1360             assert(nm != NULL, "invalid nmethod_pc");
1361 
1362             // The old patch site is now a move instruction so update
1363             // the reloc info so that it will get updated during
1364             // future GCs.
1365             RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1));
1366             relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc,
1367                                                      relocInfo::none, rtype);
1368 #ifdef SPARC
1369             // Sparc takes two relocations for an metadata so update the second one.
1370             address instr_pc2 = instr_pc + NativeMovConstReg::add_offset;
1371             RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1372             relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
1373                                                      relocInfo::none, rtype);
1374 #endif
1375 #ifdef PPC32
1376           { address instr_pc2 = instr_pc + NativeMovConstReg::lo_offset;
1377             RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1378             relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
1379                                                      relocInfo::none, rtype);
1380           }
1381 #endif
1382           }
1383 
1384         } else {
1385           ICache::invalidate_range(copy_buff, *byte_count);
1386           NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry);
1387         }
1388       }
1389     }
1390   }
1391 
1392   // If we are patching in a non-perm oop, make sure the nmethod
1393   // is on the right list.
1394   {
1395     MutexLocker ml_code (CodeCache_lock, Mutex::_no_safepoint_check_flag);
1396     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1397     guarantee(nm != NULL, "only nmethods can contain non-perm oops");
1398 
1399     // Since we've patched some oops in the nmethod,
1400     // (re)register it with the heap.
1401     Universe::heap()->register_nmethod(nm);
1402   }
1403 JRT_END
1404 
1405 #else // DEOPTIMIZE_WHEN_PATCHING
1406 
1407 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
1408   RegisterMap reg_map(thread, false);
1409 
1410   NOT_PRODUCT(_patch_code_slowcase_cnt++;)
1411   if (TracePatching) {
1412     tty->print_cr("Deoptimizing because patch is needed");
1413   }
1414 
1415   frame runtime_frame = thread->last_frame();
1416   frame caller_frame = runtime_frame.sender(&reg_map);
1417 
1418   // It's possible the nmethod was invalidated in the last
1419   // safepoint, but if it's still alive then make it not_entrant.
1420   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1421   if (nm != NULL) {
1422     nm->make_not_entrant();
1423   }
1424 
1425   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1426 
1427   // Return to the now deoptimized frame.
1428 JRT_END
1429 
1430 #endif // DEOPTIMIZE_WHEN_PATCHING
1431 
1432 //
1433 // Entry point for compiled code. We want to patch a nmethod.
1434 // We don't do a normal VM transition here because we want to
1435 // know after the patching is complete and any safepoint(s) are taken
1436 // if the calling nmethod was deoptimized. We do this by calling a
1437 // helper method which does the normal VM transition and when it
1438 // completes we can check for deoptimization. This simplifies the
1439 // assembly code in the cpu directories.
1440 //
1441 int Runtime1::move_klass_patching(JavaThread* thread) {
1442 //
1443 // NOTE: we are still in Java
1444 //
1445   Thread* THREAD = thread;
1446   debug_only(NoHandleMark nhm;)
1447   {
1448     // Enter VM mode
1449 
1450     ResetNoHandleMark rnhm;
1451     patch_code(thread, load_klass_patching_id);
1452   }
1453   // Back in JAVA, use no oops DON'T safepoint
1454 
1455   // Return true if calling code is deoptimized
1456 
1457   return caller_is_deopted();
1458 }
1459 
1460 int Runtime1::move_mirror_patching(JavaThread* thread) {
1461 //
1462 // NOTE: we are still in Java
1463 //
1464   Thread* THREAD = thread;
1465   debug_only(NoHandleMark nhm;)
1466   {
1467     // Enter VM mode
1468 
1469     ResetNoHandleMark rnhm;
1470     patch_code(thread, load_mirror_patching_id);
1471   }
1472   // Back in JAVA, use no oops DON'T safepoint
1473 
1474   // Return true if calling code is deoptimized
1475 
1476   return caller_is_deopted();
1477 }
1478 
1479 int Runtime1::move_appendix_patching(JavaThread* thread) {
1480 //
1481 // NOTE: we are still in Java
1482 //
1483   Thread* THREAD = thread;
1484   debug_only(NoHandleMark nhm;)
1485   {
1486     // Enter VM mode
1487 
1488     ResetNoHandleMark rnhm;
1489     patch_code(thread, load_appendix_patching_id);
1490   }
1491   // Back in JAVA, use no oops DON'T safepoint
1492 
1493   // Return true if calling code is deoptimized
1494 
1495   return caller_is_deopted();
1496 }
1497 //
1498 // Entry point for compiled code. We want to patch a nmethod.
1499 // We don't do a normal VM transition here because we want to
1500 // know after the patching is complete and any safepoint(s) are taken
1501 // if the calling nmethod was deoptimized. We do this by calling a
1502 // helper method which does the normal VM transition and when it
1503 // completes we can check for deoptimization. This simplifies the
1504 // assembly code in the cpu directories.
1505 //
1506 
1507 int Runtime1::access_field_patching(JavaThread* thread) {
1508 //
1509 // NOTE: we are still in Java
1510 //
1511   Thread* THREAD = thread;
1512   debug_only(NoHandleMark nhm;)
1513   {
1514     // Enter VM mode
1515 
1516     ResetNoHandleMark rnhm;
1517     patch_code(thread, access_field_patching_id);
1518   }
1519   // Back in JAVA, use no oops DON'T safepoint
1520 
1521   // Return true if calling code is deoptimized
1522 
1523   return caller_is_deopted();
1524 JRT_END
1525 
1526 
1527 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1528   // for now we just print out the block id
1529   tty->print("%d ", block_id);
1530 JRT_END
1531 
1532 
1533 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1534   // had to return int instead of bool, otherwise there may be a mismatch
1535   // between the C calling convention and the Java one.
1536   // e.g., on x86, GCC may clear only %al when returning a bool false, but
1537   // JVM takes the whole %eax as the return value, which may misinterpret
1538   // the return value as a boolean true.
1539 
1540   assert(mirror != NULL, "should null-check on mirror before calling");
1541   Klass* k = java_lang_Class::as_Klass(mirror);
1542   return (k != NULL && obj != NULL && obj->is_a(k)) ? 1 : 0;
1543 JRT_END
1544 
1545 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* thread))
1546   ResourceMark rm;
1547 
1548   assert(!TieredCompilation, "incompatible with tiered compilation");
1549 
1550   RegisterMap reg_map(thread, false);
1551   frame runtime_frame = thread->last_frame();
1552   frame caller_frame = runtime_frame.sender(&reg_map);
1553 
1554   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1555   assert (nm != NULL, "no more nmethod?");
1556   nm->make_not_entrant();
1557 
1558   methodHandle m(nm->method());
1559   MethodData* mdo = m->method_data();
1560 
1561   if (mdo == NULL && !HAS_PENDING_EXCEPTION) {
1562     // Build an MDO.  Ignore errors like OutOfMemory;
1563     // that simply means we won't have an MDO to update.
1564     Method::build_interpreter_method_data(m, THREAD);
1565     if (HAS_PENDING_EXCEPTION) {
1566       assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1567       CLEAR_PENDING_EXCEPTION;
1568     }
1569     mdo = m->method_data();
1570   }
1571 
1572   if (mdo != NULL) {
1573     mdo->inc_trap_count(Deoptimization::Reason_none);
1574   }
1575 
1576   if (TracePredicateFailedTraps) {
1577     stringStream ss1, ss2;
1578     vframeStream vfst(thread);
1579     methodHandle inlinee = methodHandle(vfst.method());
1580     inlinee->print_short_name(&ss1);
1581     m->print_short_name(&ss2);
1582     tty->print_cr("Predicate failed trap in method %s at bci %d inlined in %s at pc " INTPTR_FORMAT, ss1.as_string(), vfst.bci(), ss2.as_string(), p2i(caller_frame.pc()));
1583   }
1584 
1585 
1586   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1587 
1588 JRT_END
1589 
1590 #ifndef PRODUCT
1591 void Runtime1::print_statistics() {
1592   tty->print_cr("C1 Runtime statistics:");
1593   tty->print_cr(" _resolve_invoke_virtual_cnt:     %d", SharedRuntime::_resolve_virtual_ctr);
1594   tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr);
1595   tty->print_cr(" _resolve_invoke_static_cnt:      %d", SharedRuntime::_resolve_static_ctr);
1596   tty->print_cr(" _handle_wrong_method_cnt:        %d", SharedRuntime::_wrong_method_ctr);
1597   tty->print_cr(" _ic_miss_cnt:                    %d", SharedRuntime::_ic_miss_ctr);
1598   tty->print_cr(" _generic_arraycopy_cnt:          %d", _generic_arraycopy_cnt);
1599   tty->print_cr(" _generic_arraycopystub_cnt:      %d", _generic_arraycopystub_cnt);
1600   tty->print_cr(" _byte_arraycopy_cnt:             %d", _byte_arraycopy_stub_cnt);
1601   tty->print_cr(" _short_arraycopy_cnt:            %d", _short_arraycopy_stub_cnt);
1602   tty->print_cr(" _int_arraycopy_cnt:              %d", _int_arraycopy_stub_cnt);
1603   tty->print_cr(" _long_arraycopy_cnt:             %d", _long_arraycopy_stub_cnt);
1604   tty->print_cr(" _oop_arraycopy_cnt:              %d", _oop_arraycopy_stub_cnt);
1605   tty->print_cr(" _arraycopy_slowcase_cnt:         %d", _arraycopy_slowcase_cnt);
1606   tty->print_cr(" _arraycopy_checkcast_cnt:        %d", _arraycopy_checkcast_cnt);
1607   tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%d", _arraycopy_checkcast_attempt_cnt);
1608 
1609   tty->print_cr(" _new_type_array_slowcase_cnt:    %d", _new_type_array_slowcase_cnt);
1610   tty->print_cr(" _new_object_array_slowcase_cnt:  %d", _new_object_array_slowcase_cnt);
1611   tty->print_cr(" _new_value_array_slowcase_cnt:   %d", _new_value_array_slowcase_cnt);
1612   tty->print_cr(" _new_instance_slowcase_cnt:      %d", _new_instance_slowcase_cnt);
1613   tty->print_cr(" _new_multi_array_slowcase_cnt:   %d", _new_multi_array_slowcase_cnt);
1614   tty->print_cr(" _load_flattened_array_slowcase_cnt:   %d", _load_flattened_array_slowcase_cnt);
1615   tty->print_cr(" _store_flattened_array_slowcase_cnt:  %d", _store_flattened_array_slowcase_cnt);
1616   tty->print_cr(" _substitutability_check_slowcase_cnt: %d", _substitutability_check_slowcase_cnt);
1617   tty->print_cr(" _buffer_value_args_slowcase_cnt:%d", _buffer_value_args_slowcase_cnt);
1618   tty->print_cr(" _buffer_value_args_no_receiver_slowcase_cnt:%d", _buffer_value_args_no_receiver_slowcase_cnt);
1619 
1620   tty->print_cr(" _monitorenter_slowcase_cnt:      %d", _monitorenter_slowcase_cnt);
1621   tty->print_cr(" _monitorexit_slowcase_cnt:       %d", _monitorexit_slowcase_cnt);
1622   tty->print_cr(" _patch_code_slowcase_cnt:        %d", _patch_code_slowcase_cnt);
1623 
1624   tty->print_cr(" _throw_range_check_exception_count:            %d:", _throw_range_check_exception_count);
1625   tty->print_cr(" _throw_index_exception_count:                  %d:", _throw_index_exception_count);
1626   tty->print_cr(" _throw_div0_exception_count:                   %d:", _throw_div0_exception_count);
1627   tty->print_cr(" _throw_null_pointer_exception_count:           %d:", _throw_null_pointer_exception_count);
1628   tty->print_cr(" _throw_class_cast_exception_count:             %d:", _throw_class_cast_exception_count);
1629   tty->print_cr(" _throw_incompatible_class_change_error_count:  %d:", _throw_incompatible_class_change_error_count);
1630   tty->print_cr(" _throw_illegal_monitor_state_exception_count:  %d:", _throw_illegal_monitor_state_exception_count);
1631   tty->print_cr(" _throw_array_store_exception_count:            %d:", _throw_array_store_exception_count);
1632   tty->print_cr(" _throw_count:                                  %d:", _throw_count);
1633 
1634   SharedRuntime::print_ic_miss_histogram();
1635   tty->cr();
1636 }
1637 #endif // PRODUCT