1 /*
   2  * Copyright 1999-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_c1_Runtime1.cpp.incl"
  27 
  28 
  29 // Implementation of StubAssembler
  30 
  31 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
  32   _name = name;
  33   _must_gc_arguments = false;
  34   _frame_size = no_frame_size;
  35   _num_rt_args = 0;
  36   _stub_id = stub_id;
  37 }
  38 
  39 
  40 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
  41   _name = name;
  42   _must_gc_arguments = must_gc_arguments;
  43 }
  44 
  45 
  46 void StubAssembler::set_frame_size(int size) {
  47   if (_frame_size == no_frame_size) {
  48     _frame_size = size;
  49   }
  50   assert(_frame_size == size, "can't change the frame size");
  51 }
  52 
  53 
  54 void StubAssembler::set_num_rt_args(int args) {
  55   if (_num_rt_args == 0) {
  56     _num_rt_args = args;
  57   }
  58   assert(_num_rt_args == args, "can't change the number of args");
  59 }
  60 
  61 // Implementation of Runtime1
  62 
  63 bool      Runtime1::_is_initialized = false;
  64 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids];
  65 const char *Runtime1::_blob_names[] = {
  66   RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME)
  67 };
  68 
  69 #ifndef PRODUCT
  70 // statistics
  71 int Runtime1::_generic_arraycopy_cnt = 0;
  72 int Runtime1::_primitive_arraycopy_cnt = 0;
  73 int Runtime1::_oop_arraycopy_cnt = 0;
  74 int Runtime1::_arraycopy_slowcase_cnt = 0;
  75 int Runtime1::_new_type_array_slowcase_cnt = 0;
  76 int Runtime1::_new_object_array_slowcase_cnt = 0;
  77 int Runtime1::_new_instance_slowcase_cnt = 0;
  78 int Runtime1::_new_multi_array_slowcase_cnt = 0;
  79 int Runtime1::_monitorenter_slowcase_cnt = 0;
  80 int Runtime1::_monitorexit_slowcase_cnt = 0;
  81 int Runtime1::_patch_code_slowcase_cnt = 0;
  82 int Runtime1::_throw_range_check_exception_count = 0;
  83 int Runtime1::_throw_index_exception_count = 0;
  84 int Runtime1::_throw_div0_exception_count = 0;
  85 int Runtime1::_throw_null_pointer_exception_count = 0;
  86 int Runtime1::_throw_class_cast_exception_count = 0;
  87 int Runtime1::_throw_incompatible_class_change_error_count = 0;
  88 int Runtime1::_throw_array_store_exception_count = 0;
  89 int Runtime1::_throw_count = 0;
  90 #endif
  91 
  92 BufferBlob* Runtime1::_buffer_blob  = NULL;
  93 
  94 // Simple helper to see if the caller of a runtime stub which
  95 // entered the VM has been deoptimized
  96 
  97 static bool caller_is_deopted() {
  98   JavaThread* thread = JavaThread::current();
  99   RegisterMap reg_map(thread, false);
 100   frame runtime_frame = thread->last_frame();
 101   frame caller_frame = runtime_frame.sender(&reg_map);
 102   assert(caller_frame.is_compiled_frame(), "must be compiled");
 103   return caller_frame.is_deoptimized_frame();
 104 }
 105 
 106 // Stress deoptimization
 107 static void deopt_caller() {
 108   if ( !caller_is_deopted()) {
 109     JavaThread* thread = JavaThread::current();
 110     RegisterMap reg_map(thread, false);
 111     frame runtime_frame = thread->last_frame();
 112     frame caller_frame = runtime_frame.sender(&reg_map);
 113     VM_DeoptimizeFrame deopt(thread, caller_frame.id());
 114     VMThread::execute(&deopt);
 115     assert(caller_is_deopted(), "Must be deoptimized");
 116   }
 117 }
 118 
 119 
 120 BufferBlob* Runtime1::get_buffer_blob() {
 121   // Allocate code buffer space only once
 122   BufferBlob* blob = _buffer_blob;
 123   if (blob == NULL) {
 124     // setup CodeBuffer.  Preallocate a BufferBlob of size
 125     // NMethodSizeLimit plus some extra space for constants.
 126     int code_buffer_size = desired_max_code_buffer_size() + desired_max_constant_size();
 127     blob = BufferBlob::create("Compiler1 temporary CodeBuffer",
 128                               code_buffer_size);
 129     guarantee(blob != NULL, "must create initial code buffer");
 130     _buffer_blob = blob;
 131   }
 132   return _buffer_blob;
 133 }
 134 
 135 void Runtime1::setup_code_buffer(CodeBuffer* code, int call_stub_estimate) {
 136   // Preinitialize the consts section to some large size:
 137   int locs_buffer_size = 20 * (relocInfo::length_limit + sizeof(relocInfo));
 138   char* locs_buffer = NEW_RESOURCE_ARRAY(char, locs_buffer_size);
 139   code->insts()->initialize_shared_locs((relocInfo*)locs_buffer,
 140                                         locs_buffer_size / sizeof(relocInfo));
 141   code->initialize_consts_size(desired_max_constant_size());
 142   // Call stubs + deopt/exception handler
 143   code->initialize_stubs_size((call_stub_estimate * LIR_Assembler::call_stub_size) +
 144                               LIR_Assembler::exception_handler_size +
 145                               LIR_Assembler::deopt_handler_size);
 146 }
 147 
 148 
 149 void Runtime1::generate_blob_for(StubID id) {
 150   assert(0 <= id && id < number_of_ids, "illegal stub id");
 151   ResourceMark rm;
 152   // create code buffer for code storage
 153   CodeBuffer code(get_buffer_blob()->instructions_begin(),
 154                   get_buffer_blob()->instructions_size());
 155 
 156   setup_code_buffer(&code, 0);
 157 
 158   // create assembler for code generation
 159   StubAssembler* sasm = new StubAssembler(&code, name_for(id), id);
 160   // generate code for runtime stub
 161   OopMapSet* oop_maps;
 162   oop_maps = generate_code_for(id, sasm);
 163   assert(oop_maps == NULL || sasm->frame_size() != no_frame_size,
 164          "if stub has an oop map it must have a valid frame size");
 165 
 166 #ifdef ASSERT
 167   // Make sure that stubs that need oopmaps have them
 168   switch (id) {
 169     // These stubs don't need to have an oopmap
 170     case dtrace_object_alloc_id:
 171     case g1_pre_barrier_slow_id:
 172     case g1_post_barrier_slow_id:
 173     case slow_subtype_check_id:
 174     case fpu2long_stub_id:
 175     case unwind_exception_id:
 176 #ifndef TIERED
 177     case counter_overflow_id: // Not generated outside the tiered world
 178 #endif
 179 #ifdef SPARC
 180     case handle_exception_nofpu_id:  // Unused on sparc
 181 #endif
 182       break;
 183 
 184     // All other stubs should have oopmaps
 185     default:
 186       assert(oop_maps != NULL, "must have an oopmap");
 187   }
 188 #endif
 189 
 190   // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned)
 191   sasm->align(BytesPerWord);
 192   // make sure all code is in code buffer
 193   sasm->flush();
 194   // create blob - distinguish a few special cases
 195   CodeBlob* blob = RuntimeStub::new_runtime_stub(name_for(id),
 196                                                  &code,
 197                                                  CodeOffsets::frame_never_safe,
 198                                                  sasm->frame_size(),
 199                                                  oop_maps,
 200                                                  sasm->must_gc_arguments());
 201   // install blob
 202   assert(blob != NULL, "blob must exist");
 203   _blobs[id] = blob;
 204 }
 205 
 206 
 207 void Runtime1::initialize() {
 208   // Warning: If we have more than one compilation running in parallel, we
 209   //          need a lock here with the current setup (lazy initialization).
 210   if (!is_initialized()) {
 211     _is_initialized = true;
 212 
 213     // platform-dependent initialization
 214     initialize_pd();
 215     // generate stubs
 216     for (int id = 0; id < number_of_ids; id++) generate_blob_for((StubID)id);
 217     // printing
 218 #ifndef PRODUCT
 219     if (PrintSimpleStubs) {
 220       ResourceMark rm;
 221       for (int id = 0; id < number_of_ids; id++) {
 222         _blobs[id]->print();
 223         if (_blobs[id]->oop_maps() != NULL) {
 224           _blobs[id]->oop_maps()->print();
 225         }
 226       }
 227     }
 228 #endif
 229   }
 230 }
 231 
 232 
 233 CodeBlob* Runtime1::blob_for(StubID id) {
 234   assert(0 <= id && id < number_of_ids, "illegal stub id");
 235   if (!is_initialized()) initialize();
 236   return _blobs[id];
 237 }
 238 
 239 
 240 const char* Runtime1::name_for(StubID id) {
 241   assert(0 <= id && id < number_of_ids, "illegal stub id");
 242   return _blob_names[id];
 243 }
 244 
 245 const char* Runtime1::name_for_address(address entry) {
 246   for (int id = 0; id < number_of_ids; id++) {
 247     if (entry == entry_for((StubID)id)) return name_for((StubID)id);
 248   }
 249 
 250 #define FUNCTION_CASE(a, f) \
 251   if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f))  return #f
 252 
 253   FUNCTION_CASE(entry, os::javaTimeMillis);
 254   FUNCTION_CASE(entry, os::javaTimeNanos);
 255   FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end);
 256   FUNCTION_CASE(entry, SharedRuntime::d2f);
 257   FUNCTION_CASE(entry, SharedRuntime::d2i);
 258   FUNCTION_CASE(entry, SharedRuntime::d2l);
 259   FUNCTION_CASE(entry, SharedRuntime::dcos);
 260   FUNCTION_CASE(entry, SharedRuntime::dexp);
 261   FUNCTION_CASE(entry, SharedRuntime::dlog);
 262   FUNCTION_CASE(entry, SharedRuntime::dlog10);
 263   FUNCTION_CASE(entry, SharedRuntime::dpow);
 264   FUNCTION_CASE(entry, SharedRuntime::drem);
 265   FUNCTION_CASE(entry, SharedRuntime::dsin);
 266   FUNCTION_CASE(entry, SharedRuntime::dtan);
 267   FUNCTION_CASE(entry, SharedRuntime::f2i);
 268   FUNCTION_CASE(entry, SharedRuntime::f2l);
 269   FUNCTION_CASE(entry, SharedRuntime::frem);
 270   FUNCTION_CASE(entry, SharedRuntime::l2d);
 271   FUNCTION_CASE(entry, SharedRuntime::l2f);
 272   FUNCTION_CASE(entry, SharedRuntime::ldiv);
 273   FUNCTION_CASE(entry, SharedRuntime::lmul);
 274   FUNCTION_CASE(entry, SharedRuntime::lrem);
 275   FUNCTION_CASE(entry, SharedRuntime::lrem);
 276   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry);
 277   FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
 278   FUNCTION_CASE(entry, trace_block_entry);
 279 
 280 #undef FUNCTION_CASE
 281 
 282   return "<unknown function>";
 283 }
 284 
 285 
 286 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, klassOopDesc* klass))
 287   NOT_PRODUCT(_new_instance_slowcase_cnt++;)
 288 
 289   assert(oop(klass)->is_klass(), "not a class");
 290   instanceKlassHandle h(thread, klass);
 291   h->check_valid_for_instantiation(true, CHECK);
 292   // make sure klass is initialized
 293   h->initialize(CHECK);
 294   // allocate instance and return via TLS
 295   oop obj = h->allocate_instance(CHECK);
 296   thread->set_vm_result(obj);
 297 JRT_END
 298 
 299 
 300 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, klassOopDesc* klass, jint length))
 301   NOT_PRODUCT(_new_type_array_slowcase_cnt++;)
 302   // Note: no handle for klass needed since they are not used
 303   //       anymore after new_typeArray() and no GC can happen before.
 304   //       (This may have to change if this code changes!)
 305   assert(oop(klass)->is_klass(), "not a class");
 306   BasicType elt_type = typeArrayKlass::cast(klass)->element_type();
 307   oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 308   thread->set_vm_result(obj);
 309   // This is pretty rare but this runtime patch is stressful to deoptimization
 310   // if we deoptimize here so force a deopt to stress the path.
 311   if (DeoptimizeALot) {
 312     deopt_caller();
 313   }
 314 
 315 JRT_END
 316 
 317 
 318 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, klassOopDesc* array_klass, jint length))
 319   NOT_PRODUCT(_new_object_array_slowcase_cnt++;)
 320 
 321   // Note: no handle for klass needed since they are not used
 322   //       anymore after new_objArray() and no GC can happen before.
 323   //       (This may have to change if this code changes!)
 324   assert(oop(array_klass)->is_klass(), "not a class");
 325   klassOop elem_klass = objArrayKlass::cast(array_klass)->element_klass();
 326   objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 327   thread->set_vm_result(obj);
 328   // This is pretty rare but this runtime patch is stressful to deoptimization
 329   // if we deoptimize here so force a deopt to stress the path.
 330   if (DeoptimizeALot) {
 331     deopt_caller();
 332   }
 333 JRT_END
 334 
 335 
 336 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, klassOopDesc* klass, int rank, jint* dims))
 337   NOT_PRODUCT(_new_multi_array_slowcase_cnt++;)
 338 
 339   assert(oop(klass)->is_klass(), "not a class");
 340   assert(rank >= 1, "rank must be nonzero");
 341   oop obj = arrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 342   thread->set_vm_result(obj);
 343 JRT_END
 344 
 345 
 346 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id))
 347   tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id);
 348 JRT_END
 349 
 350 
 351 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread))
 352   THROW(vmSymbolHandles::java_lang_ArrayStoreException());
 353 JRT_END
 354 
 355 
 356 JRT_ENTRY(void, Runtime1::post_jvmti_exception_throw(JavaThread* thread))
 357   if (JvmtiExport::can_post_exceptions()) {
 358     vframeStream vfst(thread, true);
 359     address bcp = vfst.method()->bcp_from(vfst.bci());
 360     JvmtiExport::post_exception_throw(thread, vfst.method(), bcp, thread->exception_oop());
 361   }
 362 JRT_END
 363 
 364 #ifdef TIERED
 365 JRT_ENTRY(void, Runtime1::counter_overflow(JavaThread* thread, int bci))
 366   RegisterMap map(thread, false);
 367   frame fr =  thread->last_frame().sender(&map);
 368   nmethod* nm = (nmethod*) fr.cb();
 369   assert(nm!= NULL && nm->is_nmethod(), "what?");
 370   methodHandle method(thread, nm->method());
 371   if (bci == 0) {
 372     // invocation counter overflow
 373     if (!Tier1CountOnly) {
 374       CompilationPolicy::policy()->method_invocation_event(method, CHECK);
 375     } else {
 376       method()->invocation_counter()->reset();
 377     }
 378   } else {
 379     if (!Tier1CountOnly) {
 380       // Twe have a bci but not the destination bci and besides a backedge
 381       // event is more for OSR which we don't want here.
 382       CompilationPolicy::policy()->method_invocation_event(method, CHECK);
 383     } else {
 384       method()->backedge_counter()->reset();
 385     }
 386   }
 387 JRT_END
 388 #endif // TIERED
 389 
 390 extern void vm_exit(int code);
 391 
 392 // Enter this method from compiled code handler below. This is where we transition
 393 // to VM mode. This is done as a helper routine so that the method called directly
 394 // from compiled code does not have to transition to VM. This allows the entry
 395 // method to see if the nmethod that we have just looked up a handler for has
 396 // been deoptimized while we were in the vm. This simplifies the assembly code
 397 // cpu directories.
 398 //
 399 // We are entering here from exception stub (via the entry method below)
 400 // If there is a compiled exception handler in this method, we will continue there;
 401 // otherwise we will unwind the stack and continue at the caller of top frame method
 402 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 403 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 404 // check to see if the handler we are going to return is now in a nmethod that has
 405 // been deoptimized. If that is the case we return the deopt blob
 406 // unpack_with_exception entry instead. This makes life for the exception blob easier
 407 // because making that same check and diverting is painful from assembly language.
 408 //
 409 
 410 
 411 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm))
 412 
 413   Handle exception(thread, ex);
 414   nm = CodeCache::find_nmethod(pc);
 415   assert(nm != NULL, "this is not an nmethod");
 416   // Adjust the pc as needed/
 417   if (nm->is_deopt_pc(pc)) {
 418     RegisterMap map(thread, false);
 419     frame exception_frame = thread->last_frame().sender(&map);
 420     // if the frame isn't deopted then pc must not correspond to the caller of last_frame
 421     assert(exception_frame.is_deoptimized_frame(), "must be deopted");
 422     pc = exception_frame.pc();
 423   }
 424 #ifdef ASSERT
 425   assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
 426   assert(exception->is_oop(), "just checking");
 427   // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
 428   if (!(exception->is_a(SystemDictionary::throwable_klass()))) {
 429     if (ExitVMOnVerifyError) vm_exit(-1);
 430     ShouldNotReachHere();
 431   }
 432 #endif
 433 
 434   // Check the stack guard pages and reenable them if necessary and there is
 435   // enough space on the stack to do so.  Use fast exceptions only if the guard
 436   // pages are enabled.
 437   bool guard_pages_enabled = thread->stack_yellow_zone_enabled();
 438   if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
 439 
 440   if (JvmtiExport::can_post_exceptions()) {
 441     // To ensure correct notification of exception catches and throws
 442     // we have to deoptimize here.  If we attempted to notify the
 443     // catches and throws during this exception lookup it's possible
 444     // we could deoptimize on the way out of the VM and end back in
 445     // the interpreter at the throw site.  This would result in double
 446     // notifications since the interpreter would also notify about
 447     // these same catches and throws as it unwound the frame.
 448 
 449     RegisterMap reg_map(thread);
 450     frame stub_frame = thread->last_frame();
 451     frame caller_frame = stub_frame.sender(&reg_map);
 452 
 453     // We don't really want to deoptimize the nmethod itself since we
 454     // can actually continue in the exception handler ourselves but I
 455     // don't see an easy way to have the desired effect.
 456     VM_DeoptimizeFrame deopt(thread, caller_frame.id());
 457     VMThread::execute(&deopt);
 458 
 459     return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 460   }
 461 
 462   // ExceptionCache is used only for exceptions at call and not for implicit exceptions
 463   if (guard_pages_enabled) {
 464     address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
 465     if (fast_continuation != NULL) {
 466       if (fast_continuation == ExceptionCache::unwind_handler()) fast_continuation = NULL;
 467       return fast_continuation;
 468     }
 469   }
 470 
 471   // If the stack guard pages are enabled, check whether there is a handler in
 472   // the current method.  Otherwise (guard pages disabled), force an unwind and
 473   // skip the exception cache update (i.e., just leave continuation==NULL).
 474   address continuation = NULL;
 475   if (guard_pages_enabled) {
 476 
 477     // New exception handling mechanism can support inlined methods
 478     // with exception handlers since the mappings are from PC to PC
 479 
 480     // debugging support
 481     // tracing
 482     if (TraceExceptions) {
 483       ttyLocker ttyl;
 484       ResourceMark rm;
 485       tty->print_cr("Exception <%s> (0x%x) thrown in compiled method <%s> at PC " PTR_FORMAT " for thread 0x%x",
 486                     exception->print_value_string(), (address)exception(), nm->method()->print_value_string(), pc, thread);
 487     }
 488     // for AbortVMOnException flag
 489     NOT_PRODUCT(Exceptions::debug_check_abort(exception));
 490 
 491     // Clear out the exception oop and pc since looking up an
 492     // exception handler can cause class loading, which might throw an
 493     // exception and those fields are expected to be clear during
 494     // normal bytecode execution.
 495     thread->set_exception_oop(NULL);
 496     thread->set_exception_pc(NULL);
 497 
 498     continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false);
 499     // If an exception was thrown during exception dispatch, the exception oop may have changed
 500     thread->set_exception_oop(exception());
 501     thread->set_exception_pc(pc);
 502 
 503     // the exception cache is used only by non-implicit exceptions
 504     if (continuation == NULL) {
 505       nm->add_handler_for_exception_and_pc(exception, pc, ExceptionCache::unwind_handler());
 506     } else {
 507       nm->add_handler_for_exception_and_pc(exception, pc, continuation);
 508     }
 509   }
 510 
 511   thread->set_vm_result(exception());
 512 
 513   if (TraceExceptions) {
 514     ttyLocker ttyl;
 515     ResourceMark rm;
 516     tty->print_cr("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT " for exception thrown at PC " PTR_FORMAT,
 517                   thread, continuation, pc);
 518   }
 519 
 520   return continuation;
 521 JRT_END
 522 
 523 // Enter this method from compiled code only if there is a Java exception handler
 524 // in the method handling the exception
 525 // We are entering here from exception stub. We don't do a normal VM transition here.
 526 // We do it in a helper. This is so we can check to see if the nmethod we have just
 527 // searched for an exception handler has been deoptimized in the meantime.
 528 address  Runtime1::exception_handler_for_pc(JavaThread* thread) {
 529   oop exception = thread->exception_oop();
 530   address pc = thread->exception_pc();
 531   // Still in Java mode
 532   debug_only(ResetNoHandleMark rnhm);
 533   nmethod* nm = NULL;
 534   address continuation = NULL;
 535   {
 536     // Enter VM mode by calling the helper
 537 
 538     ResetNoHandleMark rnhm;
 539     continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
 540   }
 541   // Back in JAVA, use no oops DON'T safepoint
 542 
 543   // Now check to see if the nmethod we were called from is now deoptimized.
 544   // If so we must return to the deopt blob and deoptimize the nmethod
 545 
 546   if (nm != NULL && caller_is_deopted()) {
 547     continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
 548   }
 549 
 550   return continuation;
 551 }
 552 
 553 
 554 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index))
 555   NOT_PRODUCT(_throw_range_check_exception_count++;)
 556   Events::log("throw_range_check");
 557   char message[jintAsStringSize];
 558   sprintf(message, "%d", index);
 559   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message);
 560 JRT_END
 561 
 562 
 563 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index))
 564   NOT_PRODUCT(_throw_index_exception_count++;)
 565   Events::log("throw_index");
 566   char message[16];
 567   sprintf(message, "%d", index);
 568   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message);
 569 JRT_END
 570 
 571 
 572 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread))
 573   NOT_PRODUCT(_throw_div0_exception_count++;)
 574   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
 575 JRT_END
 576 
 577 
 578 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread))
 579   NOT_PRODUCT(_throw_null_pointer_exception_count++;)
 580   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
 581 JRT_END
 582 
 583 
 584 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object))
 585   NOT_PRODUCT(_throw_class_cast_exception_count++;)
 586   ResourceMark rm(thread);
 587   char* message = SharedRuntime::generate_class_cast_message(
 588     thread, Klass::cast(object->klass())->external_name());
 589   SharedRuntime::throw_and_post_jvmti_exception(
 590     thread, vmSymbols::java_lang_ClassCastException(), message);
 591 JRT_END
 592 
 593 
 594 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread))
 595   NOT_PRODUCT(_throw_incompatible_class_change_error_count++;)
 596   ResourceMark rm(thread);
 597   SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError());
 598 JRT_END
 599 
 600 
 601 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock))
 602   NOT_PRODUCT(_monitorenter_slowcase_cnt++;)
 603   if (PrintBiasedLockingStatistics) {
 604     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
 605   }
 606   Handle h_obj(thread, obj);
 607   assert(h_obj()->is_oop(), "must be NULL or an object");
 608   if (UseBiasedLocking) {
 609     // Retry fast entry if bias is revoked to avoid unnecessary inflation
 610     ObjectSynchronizer::fast_enter(h_obj, lock->lock(), true, CHECK);
 611   } else {
 612     if (UseFastLocking) {
 613       // When using fast locking, the compiled code has already tried the fast case
 614       assert(obj == lock->obj(), "must match");
 615       ObjectSynchronizer::slow_enter(h_obj, lock->lock(), THREAD);
 616     } else {
 617       lock->set_obj(obj);
 618       ObjectSynchronizer::fast_enter(h_obj, lock->lock(), false, THREAD);
 619     }
 620   }
 621 JRT_END
 622 
 623 
 624 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock))
 625   NOT_PRODUCT(_monitorexit_slowcase_cnt++;)
 626   assert(thread == JavaThread::current(), "threads must correspond");
 627   assert(thread->last_Java_sp(), "last_Java_sp must be set");
 628   // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
 629   EXCEPTION_MARK;
 630 
 631   oop obj = lock->obj();
 632   assert(obj->is_oop(), "must be NULL or an object");
 633   if (UseFastLocking) {
 634     // When using fast locking, the compiled code has already tried the fast case
 635     ObjectSynchronizer::slow_exit(obj, lock->lock(), THREAD);
 636   } else {
 637     ObjectSynchronizer::fast_exit(obj, lock->lock(), THREAD);
 638   }
 639 JRT_END
 640 
 641 
 642 static klassOop resolve_field_return_klass(methodHandle caller, int bci, TRAPS) {
 643   Bytecode_field* field_access = Bytecode_field_at(caller(), caller->bcp_from(bci));
 644   // This can be static or non-static field access
 645   Bytecodes::Code code       = field_access->code();
 646 
 647   // We must load class, initialize class and resolvethe field
 648   FieldAccessInfo result; // initialize class if needed
 649   constantPoolHandle constants(THREAD, caller->constants());
 650   LinkResolver::resolve_field(result, constants, field_access->index(), Bytecodes::java_code(code), false, CHECK_NULL);
 651   return result.klass()();
 652 }
 653 
 654 
 655 //
 656 // This routine patches sites where a class wasn't loaded or
 657 // initialized at the time the code was generated.  It handles
 658 // references to classes, fields and forcing of initialization.  Most
 659 // of the cases are straightforward and involving simply forcing
 660 // resolution of a class, rewriting the instruction stream with the
 661 // needed constant and replacing the call in this function with the
 662 // patched code.  The case for static field is more complicated since
 663 // the thread which is in the process of initializing a class can
 664 // access it's static fields but other threads can't so the code
 665 // either has to deoptimize when this case is detected or execute a
 666 // check that the current thread is the initializing thread.  The
 667 // current
 668 //
 669 // Patches basically look like this:
 670 //
 671 //
 672 // patch_site: jmp patch stub     ;; will be patched
 673 // continue:   ...
 674 //             ...
 675 //             ...
 676 //             ...
 677 //
 678 // They have a stub which looks like this:
 679 //
 680 //             ;; patch body
 681 //             movl <const>, reg           (for class constants)
 682 //        <or> movl [reg1 + <const>], reg  (for field offsets)
 683 //        <or> movl reg, [reg1 + <const>]  (for field offsets)
 684 //             <being_init offset> <bytes to copy> <bytes to skip>
 685 // patch_stub: call Runtime1::patch_code (through a runtime stub)
 686 //             jmp patch_site
 687 //
 688 //
 689 // A normal patch is done by rewriting the patch body, usually a move,
 690 // and then copying it into place over top of the jmp instruction
 691 // being careful to flush caches and doing it in an MP-safe way.  The
 692 // constants following the patch body are used to find various pieces
 693 // of the patch relative to the call site for Runtime1::patch_code.
 694 // The case for getstatic and putstatic is more complicated because
 695 // getstatic and putstatic have special semantics when executing while
 696 // the class is being initialized.  getstatic/putstatic on a class
 697 // which is being_initialized may be executed by the initializing
 698 // thread but other threads have to block when they execute it.  This
 699 // is accomplished in compiled code by executing a test of the current
 700 // thread against the initializing thread of the class.  It's emitted
 701 // as boilerplate in their stub which allows the patched code to be
 702 // executed before it's copied back into the main body of the nmethod.
 703 //
 704 // being_init: get_thread(<tmp reg>
 705 //             cmpl [reg1 + <init_thread_offset>], <tmp reg>
 706 //             jne patch_stub
 707 //             movl [reg1 + <const>], reg  (for field offsets)  <or>
 708 //             movl reg, [reg1 + <const>]  (for field offsets)
 709 //             jmp continue
 710 //             <being_init offset> <bytes to copy> <bytes to skip>
 711 // patch_stub: jmp Runtim1::patch_code (through a runtime stub)
 712 //             jmp patch_site
 713 //
 714 // If the class is being initialized the patch body is rewritten and
 715 // the patch site is rewritten to jump to being_init, instead of
 716 // patch_stub.  Whenever this code is executed it checks the current
 717 // thread against the intializing thread so other threads will enter
 718 // the runtime and end up blocked waiting the class to finish
 719 // initializing inside the calls to resolve_field below.  The
 720 // initializing class will continue on it's way.  Once the class is
 721 // fully_initialized, the intializing_thread of the class becomes
 722 // NULL, so the next thread to execute this code will fail the test,
 723 // call into patch_code and complete the patching process by copying
 724 // the patch body back into the main part of the nmethod and resume
 725 // executing.
 726 //
 727 //
 728 
 729 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
 730   NOT_PRODUCT(_patch_code_slowcase_cnt++;)
 731 
 732   ResourceMark rm(thread);
 733   RegisterMap reg_map(thread, false);
 734   frame runtime_frame = thread->last_frame();
 735   frame caller_frame = runtime_frame.sender(&reg_map);
 736 
 737   // last java frame on stack
 738   vframeStream vfst(thread, true);
 739   assert(!vfst.at_end(), "Java frame must exist");
 740 
 741   methodHandle caller_method(THREAD, vfst.method());
 742   // Note that caller_method->code() may not be same as caller_code because of OSR's
 743   // Note also that in the presence of inlining it is not guaranteed
 744   // that caller_method() == caller_code->method()
 745 
 746 
 747   int bci = vfst.bci();
 748 
 749   Events::log("patch_code @ " INTPTR_FORMAT , caller_frame.pc());
 750 
 751   Bytecodes::Code code = Bytecode_at(caller_method->bcp_from(bci))->java_code();
 752 
 753 #ifndef PRODUCT
 754   // this is used by assertions in the access_field_patching_id
 755   BasicType patch_field_type = T_ILLEGAL;
 756 #endif // PRODUCT
 757   bool deoptimize_for_volatile = false;
 758   int patch_field_offset = -1;
 759   KlassHandle init_klass(THREAD, klassOop(NULL)); // klass needed by access_field_patching code
 760   Handle load_klass(THREAD, NULL);                // oop needed by load_klass_patching code
 761   if (stub_id == Runtime1::access_field_patching_id) {
 762 
 763     Bytecode_field* field_access = Bytecode_field_at(caller_method(), caller_method->bcp_from(bci));
 764     FieldAccessInfo result; // initialize class if needed
 765     Bytecodes::Code code = field_access->code();
 766     constantPoolHandle constants(THREAD, caller_method->constants());
 767     LinkResolver::resolve_field(result, constants, field_access->index(), Bytecodes::java_code(code), false, CHECK);
 768     patch_field_offset = result.field_offset();
 769 
 770     // If we're patching a field which is volatile then at compile it
 771     // must not have been know to be volatile, so the generated code
 772     // isn't correct for a volatile reference.  The nmethod has to be
 773     // deoptimized so that the code can be regenerated correctly.
 774     // This check is only needed for access_field_patching since this
 775     // is the path for patching field offsets.  load_klass is only
 776     // used for patching references to oops which don't need special
 777     // handling in the volatile case.
 778     deoptimize_for_volatile = result.access_flags().is_volatile();
 779 
 780 #ifndef PRODUCT
 781     patch_field_type = result.field_type();
 782 #endif
 783   } else if (stub_id == Runtime1::load_klass_patching_id) {
 784     oop k;
 785     switch (code) {
 786       case Bytecodes::_putstatic:
 787       case Bytecodes::_getstatic:
 788         { klassOop klass = resolve_field_return_klass(caller_method, bci, CHECK);
 789           // Save a reference to the class that has to be checked for initialization
 790           init_klass = KlassHandle(THREAD, klass);
 791           k = klass;
 792         }
 793         break;
 794       case Bytecodes::_new:
 795         { Bytecode_new* bnew = Bytecode_new_at(caller_method->bcp_from(bci));
 796           k = caller_method->constants()->klass_at(bnew->index(), CHECK);
 797         }
 798         break;
 799       case Bytecodes::_multianewarray:
 800         { Bytecode_multianewarray* mna = Bytecode_multianewarray_at(caller_method->bcp_from(bci));
 801           k = caller_method->constants()->klass_at(mna->index(), CHECK);
 802         }
 803         break;
 804       case Bytecodes::_instanceof:
 805         { Bytecode_instanceof* io = Bytecode_instanceof_at(caller_method->bcp_from(bci));
 806           k = caller_method->constants()->klass_at(io->index(), CHECK);
 807         }
 808         break;
 809       case Bytecodes::_checkcast:
 810         { Bytecode_checkcast* cc = Bytecode_checkcast_at(caller_method->bcp_from(bci));
 811           k = caller_method->constants()->klass_at(cc->index(), CHECK);
 812         }
 813         break;
 814       case Bytecodes::_anewarray:
 815         { Bytecode_anewarray* anew = Bytecode_anewarray_at(caller_method->bcp_from(bci));
 816           klassOop ek = caller_method->constants()->klass_at(anew->index(), CHECK);
 817           k = Klass::cast(ek)->array_klass(CHECK);
 818         }
 819         break;
 820       case Bytecodes::_ldc:
 821       case Bytecodes::_ldc_w:
 822         {
 823           Bytecode_loadconstant* cc = Bytecode_loadconstant_at(caller_method(),
 824                                                                caller_method->bcp_from(bci));
 825           klassOop resolved = caller_method->constants()->klass_at(cc->index(), CHECK);
 826           // ldc wants the java mirror.
 827           k = resolved->klass_part()->java_mirror();
 828         }
 829         break;
 830       default: Unimplemented();
 831     }
 832     // convert to handle
 833     load_klass = Handle(THREAD, k);
 834   } else {
 835     ShouldNotReachHere();
 836   }
 837 
 838   if (deoptimize_for_volatile) {
 839     // At compile time we assumed the field wasn't volatile but after
 840     // loading it turns out it was volatile so we have to throw the
 841     // compiled code out and let it be regenerated.
 842     if (TracePatching) {
 843       tty->print_cr("Deoptimizing for patching volatile field reference");
 844     }
 845     // It's possible the nmethod was invalidated in the last
 846     // safepoint, but if it's still alive then make it not_entrant.
 847     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
 848     if (nm != NULL) {
 849       nm->make_not_entrant();
 850     }
 851 
 852     VM_DeoptimizeFrame deopt(thread, caller_frame.id());
 853     VMThread::execute(&deopt);
 854 
 855     // Return to the now deoptimized frame.
 856   }
 857 
 858 
 859   // Now copy code back
 860 
 861   {
 862     MutexLockerEx ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag);
 863     //
 864     // Deoptimization may have happened while we waited for the lock.
 865     // In that case we don't bother to do any patching we just return
 866     // and let the deopt happen
 867     if (!caller_is_deopted()) {
 868       NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc());
 869       address instr_pc = jump->jump_destination();
 870       NativeInstruction* ni = nativeInstruction_at(instr_pc);
 871       if (ni->is_jump() ) {
 872         // the jump has not been patched yet
 873         // The jump destination is slow case and therefore not part of the stubs
 874         // (stubs are only for StaticCalls)
 875 
 876         // format of buffer
 877         //    ....
 878         //    instr byte 0     <-- copy_buff
 879         //    instr byte 1
 880         //    ..
 881         //    instr byte n-1
 882         //      n
 883         //    ....             <-- call destination
 884 
 885         address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset();
 886         unsigned char* byte_count = (unsigned char*) (stub_location - 1);
 887         unsigned char* byte_skip = (unsigned char*) (stub_location - 2);
 888         unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3);
 889         address copy_buff = stub_location - *byte_skip - *byte_count;
 890         address being_initialized_entry = stub_location - *being_initialized_entry_offset;
 891         if (TracePatching) {
 892           tty->print_cr(" Patching %s at bci %d at address 0x%x  (%s)", Bytecodes::name(code), bci,
 893                         instr_pc, (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass");
 894           nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc());
 895           assert(caller_code != NULL, "nmethod not found");
 896 
 897           // NOTE we use pc() not original_pc() because we already know they are
 898           // identical otherwise we'd have never entered this block of code
 899 
 900           OopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc());
 901           assert(map != NULL, "null check");
 902           map->print();
 903           tty->cr();
 904 
 905           Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
 906         }
 907         // depending on the code below, do_patch says whether to copy the patch body back into the nmethod
 908         bool do_patch = true;
 909         if (stub_id == Runtime1::access_field_patching_id) {
 910           // The offset may not be correct if the class was not loaded at code generation time.
 911           // Set it now.
 912           NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff);
 913           assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type");
 914           assert(patch_field_offset >= 0, "illegal offset");
 915           n_move->add_offset_in_bytes(patch_field_offset);
 916         } else if (stub_id == Runtime1::load_klass_patching_id) {
 917           // If a getstatic or putstatic is referencing a klass which
 918           // isn't fully initialized, the patch body isn't copied into
 919           // place until initialization is complete.  In this case the
 920           // patch site is setup so that any threads besides the
 921           // initializing thread are forced to come into the VM and
 922           // block.
 923           do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) ||
 924                      instanceKlass::cast(init_klass())->is_initialized();
 925           NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc);
 926           if (jump->jump_destination() == being_initialized_entry) {
 927             assert(do_patch == true, "initialization must be complete at this point");
 928           } else {
 929             // patch the instruction <move reg, klass>
 930             NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
 931             assert(n_copy->data() == 0, "illegal init value");
 932             assert(load_klass() != NULL, "klass not set");
 933             n_copy->set_data((intx) (load_klass()));
 934 
 935             if (TracePatching) {
 936               Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
 937             }
 938 
 939 #ifdef SPARC
 940             // Update the oop location in the nmethod with the proper
 941             // oop.  When the code was generated, a NULL was stuffed
 942             // in the oop table and that table needs to be update to
 943             // have the right value.  On intel the value is kept
 944             // directly in the instruction instead of in the oop
 945             // table, so set_data above effectively updated the value.
 946             nmethod* nm = CodeCache::find_nmethod(instr_pc);
 947             assert(nm != NULL, "invalid nmethod_pc");
 948             RelocIterator oops(nm, copy_buff, copy_buff + 1);
 949             bool found = false;
 950             while (oops.next() && !found) {
 951               if (oops.type() == relocInfo::oop_type) {
 952                 oop_Relocation* r = oops.oop_reloc();
 953                 oop* oop_adr = r->oop_addr();
 954                 *oop_adr = load_klass();
 955                 r->fix_oop_relocation();
 956                 found = true;
 957               }
 958             }
 959             assert(found, "the oop must exist!");
 960 #endif
 961 
 962           }
 963         } else {
 964           ShouldNotReachHere();
 965         }
 966         if (do_patch) {
 967           // replace instructions
 968           // first replace the tail, then the call
 969           for (int i = NativeCall::instruction_size; i < *byte_count; i++) {
 970             address ptr = copy_buff + i;
 971             int a_byte = (*ptr) & 0xFF;
 972             address dst = instr_pc + i;
 973             *(unsigned char*)dst = (unsigned char) a_byte;
 974           }
 975           ICache::invalidate_range(instr_pc, *byte_count);
 976           NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff);
 977 
 978           if (stub_id == Runtime1::load_klass_patching_id) {
 979             // update relocInfo to oop
 980             nmethod* nm = CodeCache::find_nmethod(instr_pc);
 981             assert(nm != NULL, "invalid nmethod_pc");
 982 
 983             // The old patch site is now a move instruction so update
 984             // the reloc info so that it will get updated during
 985             // future GCs.
 986             RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1));
 987             relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc,
 988                                                      relocInfo::none, relocInfo::oop_type);
 989 #ifdef SPARC
 990             // Sparc takes two relocations for an oop so update the second one.
 991             address instr_pc2 = instr_pc + NativeMovConstReg::add_offset;
 992             RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
 993             relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
 994                                                      relocInfo::none, relocInfo::oop_type);
 995 #endif
 996           }
 997 
 998         } else {
 999           ICache::invalidate_range(copy_buff, *byte_count);
1000           NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry);
1001         }
1002       }
1003     }
1004   }
1005 JRT_END
1006 
1007 //
1008 // Entry point for compiled code. We want to patch a nmethod.
1009 // We don't do a normal VM transition here because we want to
1010 // know after the patching is complete and any safepoint(s) are taken
1011 // if the calling nmethod was deoptimized. We do this by calling a
1012 // helper method which does the normal VM transition and when it
1013 // completes we can check for deoptimization. This simplifies the
1014 // assembly code in the cpu directories.
1015 //
1016 int Runtime1::move_klass_patching(JavaThread* thread) {
1017 //
1018 // NOTE: we are still in Java
1019 //
1020   Thread* THREAD = thread;
1021   debug_only(NoHandleMark nhm;)
1022   {
1023     // Enter VM mode
1024 
1025     ResetNoHandleMark rnhm;
1026     patch_code(thread, load_klass_patching_id);
1027   }
1028   // Back in JAVA, use no oops DON'T safepoint
1029 
1030   // Return true if calling code is deoptimized
1031 
1032   return caller_is_deopted();
1033 }
1034 
1035 //
1036 // Entry point for compiled code. We want to patch a nmethod.
1037 // We don't do a normal VM transition here because we want to
1038 // know after the patching is complete and any safepoint(s) are taken
1039 // if the calling nmethod was deoptimized. We do this by calling a
1040 // helper method which does the normal VM transition and when it
1041 // completes we can check for deoptimization. This simplifies the
1042 // assembly code in the cpu directories.
1043 //
1044 
1045 int Runtime1::access_field_patching(JavaThread* thread) {
1046 //
1047 // NOTE: we are still in Java
1048 //
1049   Thread* THREAD = thread;
1050   debug_only(NoHandleMark nhm;)
1051   {
1052     // Enter VM mode
1053 
1054     ResetNoHandleMark rnhm;
1055     patch_code(thread, access_field_patching_id);
1056   }
1057   // Back in JAVA, use no oops DON'T safepoint
1058 
1059   // Return true if calling code is deoptimized
1060 
1061   return caller_is_deopted();
1062 JRT_END
1063 
1064 
1065 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1066   // for now we just print out the block id
1067   tty->print("%d ", block_id);
1068 JRT_END
1069 
1070 
1071 // Array copy return codes.
1072 enum {
1073   ac_failed = -1, // arraycopy failed
1074   ac_ok = 0       // arraycopy succeeded
1075 };
1076 
1077 
1078 template <class T> int obj_arraycopy_work(oopDesc* src, T* src_addr,
1079                                           oopDesc* dst, T* dst_addr,
1080                                           int length) {
1081 
1082   // For performance reasons, we assume we are using a card marking write
1083   // barrier. The assert will fail if this is not the case.
1084   // Note that we use the non-virtual inlineable variant of write_ref_array.
1085   BarrierSet* bs = Universe::heap()->barrier_set();
1086   assert(bs->has_write_ref_array_opt(),
1087          "Barrier set must have ref array opt");
1088   if (src == dst) {
1089     // same object, no check
1090     Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
1091     bs->write_ref_array(MemRegion((HeapWord*)dst_addr,
1092                                   (HeapWord*)(dst_addr + length)));
1093     return ac_ok;
1094   } else {
1095     klassOop bound = objArrayKlass::cast(dst->klass())->element_klass();
1096     klassOop stype = objArrayKlass::cast(src->klass())->element_klass();
1097     if (stype == bound || Klass::cast(stype)->is_subtype_of(bound)) {
1098       // Elements are guaranteed to be subtypes, so no check necessary
1099       Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
1100       bs->write_ref_array(MemRegion((HeapWord*)dst_addr,
1101                                     (HeapWord*)(dst_addr + length)));
1102       return ac_ok;
1103     }
1104   }
1105   return ac_failed;
1106 }
1107 
1108 // fast and direct copy of arrays; returning -1, means that an exception may be thrown
1109 // and we did not copy anything
1110 JRT_LEAF(int, Runtime1::arraycopy(oopDesc* src, int src_pos, oopDesc* dst, int dst_pos, int length))
1111 #ifndef PRODUCT
1112   _generic_arraycopy_cnt++;        // Slow-path oop array copy
1113 #endif
1114 
1115   if (src == NULL || dst == NULL || src_pos < 0 || dst_pos < 0 || length < 0) return ac_failed;
1116   if (!dst->is_array() || !src->is_array()) return ac_failed;
1117   if ((unsigned int) arrayOop(src)->length() < (unsigned int)src_pos + (unsigned int)length) return ac_failed;
1118   if ((unsigned int) arrayOop(dst)->length() < (unsigned int)dst_pos + (unsigned int)length) return ac_failed;
1119 
1120   if (length == 0) return ac_ok;
1121   if (src->is_typeArray()) {
1122     const klassOop klass_oop = src->klass();
1123     if (klass_oop != dst->klass()) return ac_failed;
1124     typeArrayKlass* klass = typeArrayKlass::cast(klass_oop);
1125     const int l2es = klass->log2_element_size();
1126     const int ihs = klass->array_header_in_bytes() / wordSize;
1127     char* src_addr = (char*) ((oopDesc**)src + ihs) + (src_pos << l2es);
1128     char* dst_addr = (char*) ((oopDesc**)dst + ihs) + (dst_pos << l2es);
1129     // Potential problem: memmove is not guaranteed to be word atomic
1130     // Revisit in Merlin
1131     memmove(dst_addr, src_addr, length << l2es);
1132     return ac_ok;
1133   } else if (src->is_objArray() && dst->is_objArray()) {
1134     if (UseCompressedOops) {  // will need for tiered
1135       narrowOop *src_addr  = objArrayOop(src)->obj_at_addr<narrowOop>(src_pos);
1136       narrowOop *dst_addr  = objArrayOop(dst)->obj_at_addr<narrowOop>(dst_pos);
1137       return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
1138     } else {
1139       oop *src_addr  = objArrayOop(src)->obj_at_addr<oop>(src_pos);
1140       oop *dst_addr  = objArrayOop(dst)->obj_at_addr<oop>(dst_pos);
1141       return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
1142     }
1143   }
1144   return ac_failed;
1145 JRT_END
1146 
1147 
1148 JRT_LEAF(void, Runtime1::primitive_arraycopy(HeapWord* src, HeapWord* dst, int length))
1149 #ifndef PRODUCT
1150   _primitive_arraycopy_cnt++;
1151 #endif
1152 
1153   if (length == 0) return;
1154   // Not guaranteed to be word atomic, but that doesn't matter
1155   // for anything but an oop array, which is covered by oop_arraycopy.
1156   Copy::conjoint_bytes(src, dst, length);
1157 JRT_END
1158 
1159 JRT_LEAF(void, Runtime1::oop_arraycopy(HeapWord* src, HeapWord* dst, int num))
1160 #ifndef PRODUCT
1161   _oop_arraycopy_cnt++;
1162 #endif
1163 
1164   if (num == 0) return;
1165   Copy::conjoint_oops_atomic((oop*) src, (oop*) dst, num);
1166   BarrierSet* bs = Universe::heap()->barrier_set();
1167   bs->write_ref_array(MemRegion(dst, dst + num));
1168 JRT_END
1169 
1170 
1171 #ifndef PRODUCT
1172 void Runtime1::print_statistics() {
1173   tty->print_cr("C1 Runtime statistics:");
1174   tty->print_cr(" _resolve_invoke_virtual_cnt:     %d", SharedRuntime::_resolve_virtual_ctr);
1175   tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr);
1176   tty->print_cr(" _resolve_invoke_static_cnt:      %d", SharedRuntime::_resolve_static_ctr);
1177   tty->print_cr(" _handle_wrong_method_cnt:        %d", SharedRuntime::_wrong_method_ctr);
1178   tty->print_cr(" _ic_miss_cnt:                    %d", SharedRuntime::_ic_miss_ctr);
1179   tty->print_cr(" _generic_arraycopy_cnt:          %d", _generic_arraycopy_cnt);
1180   tty->print_cr(" _primitive_arraycopy_cnt:        %d", _primitive_arraycopy_cnt);
1181   tty->print_cr(" _oop_arraycopy_cnt:              %d", _oop_arraycopy_cnt);
1182   tty->print_cr(" _arraycopy_slowcase_cnt:         %d", _arraycopy_slowcase_cnt);
1183 
1184   tty->print_cr(" _new_type_array_slowcase_cnt:    %d", _new_type_array_slowcase_cnt);
1185   tty->print_cr(" _new_object_array_slowcase_cnt:  %d", _new_object_array_slowcase_cnt);
1186   tty->print_cr(" _new_instance_slowcase_cnt:      %d", _new_instance_slowcase_cnt);
1187   tty->print_cr(" _new_multi_array_slowcase_cnt:   %d", _new_multi_array_slowcase_cnt);
1188   tty->print_cr(" _monitorenter_slowcase_cnt:      %d", _monitorenter_slowcase_cnt);
1189   tty->print_cr(" _monitorexit_slowcase_cnt:       %d", _monitorexit_slowcase_cnt);
1190   tty->print_cr(" _patch_code_slowcase_cnt:        %d", _patch_code_slowcase_cnt);
1191 
1192   tty->print_cr(" _throw_range_check_exception_count:            %d:", _throw_range_check_exception_count);
1193   tty->print_cr(" _throw_index_exception_count:                  %d:", _throw_index_exception_count);
1194   tty->print_cr(" _throw_div0_exception_count:                   %d:", _throw_div0_exception_count);
1195   tty->print_cr(" _throw_null_pointer_exception_count:           %d:", _throw_null_pointer_exception_count);
1196   tty->print_cr(" _throw_class_cast_exception_count:             %d:", _throw_class_cast_exception_count);
1197   tty->print_cr(" _throw_incompatible_class_change_error_count:  %d:", _throw_incompatible_class_change_error_count);
1198   tty->print_cr(" _throw_array_store_exception_count:            %d:", _throw_array_store_exception_count);
1199   tty->print_cr(" _throw_count:                                  %d:", _throw_count);
1200 
1201   SharedRuntime::print_ic_miss_histogram();
1202   tty->cr();
1203 }
1204 #endif // PRODUCT