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