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