1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledMethod.inline.hpp"
  30 #include "code/compiledIC.hpp"
  31 #include "code/icBuffer.hpp"
  32 #include "code/nmethod.hpp"
  33 #include "code/pcDesc.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/compilerDefinitions.inline.hpp"
  38 #include "compiler/oopMap.hpp"
  39 #include "gc/g1/heapRegion.hpp"
  40 #include "gc/shared/barrierSet.hpp"
  41 #include "gc/shared/collectedHeap.hpp"
  42 #include "gc/shared/gcLocker.hpp"
  43 #include "interpreter/bytecode.hpp"
  44 #include "interpreter/interpreter.hpp"
  45 #include "interpreter/linkResolver.hpp"
  46 #include "logging/log.hpp"
  47 #include "logging/logStream.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "oops/objArrayKlass.hpp"
  51 #include "oops/klass.inline.hpp"
  52 #include "oops/oop.inline.hpp"
  53 #include "oops/typeArrayOop.inline.hpp"
  54 #include "opto/ad.hpp"
  55 #include "opto/addnode.hpp"
  56 #include "opto/callnode.hpp"
  57 #include "opto/cfgnode.hpp"
  58 #include "opto/graphKit.hpp"
  59 #include "opto/machnode.hpp"
  60 #include "opto/matcher.hpp"
  61 #include "opto/memnode.hpp"
  62 #include "opto/mulnode.hpp"
  63 #include "opto/output.hpp"
  64 #include "opto/runtime.hpp"
  65 #include "opto/subnode.hpp"
  66 #include "prims/jvmtiExport.hpp"
  67 #include "runtime/atomic.hpp"
  68 #include "runtime/frame.inline.hpp"
  69 #include "runtime/handles.inline.hpp"
  70 #include "runtime/interfaceSupport.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/perfData.inline.hpp"
  74 #include "runtime/sharedRuntime.hpp"
  75 #include "runtime/signature.hpp"
  76 #include "runtime/stackWatermarkSet.hpp"
  77 #include "runtime/synchronizer.hpp"
  78 #include "runtime/threadCritical.hpp"
  79 #include "runtime/threadWXSetters.inline.hpp"
  80 #include "runtime/vframe.hpp"
  81 #include "runtime/vframeArray.hpp"
  82 #include "runtime/vframe_hp.hpp"
  83 #include "services/management.hpp"
  84 #include "utilities/copy.hpp"
  85 #include "utilities/preserveException.hpp"
  86 
  87 
  88 // For debugging purposes:
  89 //  To force FullGCALot inside a runtime function, add the following two lines
  90 //
  91 //  Universe::release_fullgc_alot_dummy();
  92 //  MarkSweep::invoke(0, "Debugging");
  93 //
  94 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  95 
  96 
  97 
  98 
  99 // Compiled code entry points
 100 address OptoRuntime::_new_instance_Java                           = nullptr;
 101 address OptoRuntime::_new_array_Java                              = nullptr;
 102 address OptoRuntime::_new_array_nozero_Java                       = nullptr;
 103 address OptoRuntime::_multianewarray2_Java                        = nullptr;
 104 address OptoRuntime::_multianewarray3_Java                        = nullptr;
 105 address OptoRuntime::_multianewarray4_Java                        = nullptr;
 106 address OptoRuntime::_multianewarray5_Java                        = nullptr;
 107 address OptoRuntime::_multianewarrayN_Java                        = nullptr;
 108 address OptoRuntime::_vtable_must_compile_Java                    = nullptr;
 109 address OptoRuntime::_complete_monitor_locking_Java               = nullptr;
 110 address OptoRuntime::_monitor_notify_Java                         = nullptr;
 111 address OptoRuntime::_monitor_notifyAll_Java                      = nullptr;
 112 address OptoRuntime::_rethrow_Java                                = nullptr;
 113 
 114 address OptoRuntime::_slow_arraycopy_Java                         = nullptr;
 115 address OptoRuntime::_register_finalizer_Java                     = nullptr;
 116 address OptoRuntime::_class_init_barrier_Java                     = nullptr;
 117 #if INCLUDE_JVMTI
 118 address OptoRuntime::_notify_jvmti_vthread_start                  = nullptr;
 119 address OptoRuntime::_notify_jvmti_vthread_end                    = nullptr;
 120 address OptoRuntime::_notify_jvmti_vthread_mount                  = nullptr;
 121 address OptoRuntime::_notify_jvmti_vthread_unmount                = nullptr;
 122 #endif
 123 
 124 ExceptionBlob* OptoRuntime::_exception_blob;
 125 
 126 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
 127 
 128 // This should be called in an assertion at the start of OptoRuntime routines
 129 // which are entered from compiled code (all of them)
 130 #ifdef ASSERT
 131 static bool check_compiled_frame(JavaThread* thread) {
 132   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 133   RegisterMap map(thread,
 134                   RegisterMap::UpdateMap::skip,
 135                   RegisterMap::ProcessFrames::include,
 136                   RegisterMap::WalkContinuation::skip);
 137   frame caller = thread->last_frame().sender(&map);
 138   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 139   return true;
 140 }
 141 #endif // ASSERT
 142 
 143 
 144 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 145   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 146   if (var == nullptr) { return false; }
 147 
 148 bool OptoRuntime::generate(ciEnv* env) {
 149   init_counters();
 150 
 151   generate_exception_blob();
 152 
 153   // Note: tls: Means fetching the return oop out of the thread-local storage
 154   //
 155   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,retpc
 156   // -------------------------------------------------------------------------------------------------------------------------------
 157   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true, false);
 158   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true, false);
 159   gen(env, _new_array_nozero_Java          , new_array_Type               , new_array_nozero_C              ,    0 , true, false);
 160   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true, false);
 161   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true, false);
 162   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true, false);
 163   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true, false);
 164   gen(env, _multianewarrayN_Java           , multianewarrayN_Type         , multianewarrayN_C               ,    0 , true, false);
 165 #if INCLUDE_JVMTI
 166   gen(env, _notify_jvmti_vthread_start     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_start, 0, true, false);
 167   gen(env, _notify_jvmti_vthread_end       , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_end,   0, true, false);
 168   gen(env, _notify_jvmti_vthread_mount     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_mount, 0, true, false);
 169   gen(env, _notify_jvmti_vthread_unmount   , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_unmount, 0, true, false);
 170 #endif
 171   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C, 0, false, false);
 172   gen(env, _monitor_notify_Java            , monitor_notify_Type          , monitor_notify_C                ,    0 , false, false);
 173   gen(env, _monitor_notifyAll_Java         , monitor_notify_Type          , monitor_notifyAll_C             ,    0 , false, false);
 174   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , true );
 175 
 176   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false);
 177   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false);
 178   gen(env, _class_init_barrier_Java        , class_init_barrier_Type      , class_init_barrier              ,    0 , false, false);
 179 
 180   return true;
 181 }
 182 
 183 #undef gen
 184 
 185 
 186 // Helper method to do generation of RunTimeStub's
 187 address OptoRuntime::generate_stub(ciEnv* env,
 188                                    TypeFunc_generator gen, address C_function,
 189                                    const char *name, int is_fancy_jump,
 190                                    bool pass_tls,
 191                                    bool return_pc) {
 192 
 193   // Matching the default directive, we currently have no method to match.
 194   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompilerThread::current()->compiler());
 195   ResourceMark rm;
 196   Compile C(env, gen, C_function, name, is_fancy_jump, pass_tls, return_pc, directive);
 197   DirectivesStack::release(directive);
 198   return  C.stub_entry_point();
 199 }
 200 
 201 const char* OptoRuntime::stub_name(address entry) {
 202 #ifndef PRODUCT
 203   CodeBlob* cb = CodeCache::find_blob(entry);
 204   RuntimeStub* rs =(RuntimeStub *)cb;
 205   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 206   return rs->name();
 207 #else
 208   // Fast implementation for product mode (maybe it should be inlined too)
 209   return "runtime stub";
 210 #endif
 211 }
 212 
 213 
 214 //=============================================================================
 215 // Opto compiler runtime routines
 216 //=============================================================================
 217 
 218 
 219 //=============================allocation======================================
 220 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 221 // and try allocation again.
 222 
 223 // object allocation
 224 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_instance_C, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 225   JRT_BLOCK;
 226 #ifndef PRODUCT
 227   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 228 #endif
 229   assert(check_compiled_frame(current), "incorrect caller");
 230 
 231   // These checks are cheap to make and support reflective allocation.
 232   int lh = klass->layout_helper();
 233   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 234     Handle holder(current, klass->klass_holder()); // keep the klass alive
 235     klass->check_valid_for_instantiation(false, THREAD);
 236     if (!HAS_PENDING_EXCEPTION) {
 237       InstanceKlass::cast(klass)->initialize(THREAD);
 238     }
 239   }
 240 
 241   if (!HAS_PENDING_EXCEPTION) {
 242     // Scavenge and allocate an instance.
 243     Handle holder(current, klass->klass_holder()); // keep the klass alive
 244     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 245     current->set_vm_result(result);
 246 
 247     // Pass oops back through thread local storage.  Our apparent type to Java
 248     // is that we return an oop, but we can block on exit from this routine and
 249     // a GC can trash the oop in C's return register.  The generated stub will
 250     // fetch the oop from TLS after any possible GC.
 251   }
 252 
 253   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 254   JRT_BLOCK_END;
 255 
 256   // inform GC that we won't do card marks for initializing writes.
 257   SharedRuntime::on_slowpath_allocation_exit(current);
 258 JRT_END
 259 
 260 
 261 // array allocation
 262 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_C, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 263   JRT_BLOCK;
 264 #ifndef PRODUCT
 265   SharedRuntime::_new_array_ctr++;            // new array requires GC
 266 #endif
 267   assert(check_compiled_frame(current), "incorrect caller");
 268 
 269   // Scavenge and allocate an instance.
 270   oop result;
 271 
 272   if (array_type->is_typeArray_klass()) {
 273     // The oopFactory likes to work with the element type.
 274     // (We could bypass the oopFactory, since it doesn't add much value.)
 275     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 276     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 277   } else {
 278     // Although the oopFactory likes to work with the elem_type,
 279     // the compiler prefers the array_type, since it must already have
 280     // that latter value in hand for the fast path.
 281     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 282     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 283     result = oopFactory::new_objArray(elem_type, len, THREAD);
 284   }
 285 
 286   // Pass oops back through thread local storage.  Our apparent type to Java
 287   // is that we return an oop, but we can block on exit from this routine and
 288   // a GC can trash the oop in C's return register.  The generated stub will
 289   // fetch the oop from TLS after any possible GC.
 290   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 291   current->set_vm_result(result);
 292   JRT_BLOCK_END;
 293 
 294   // inform GC that we won't do card marks for initializing writes.
 295   SharedRuntime::on_slowpath_allocation_exit(current);
 296 JRT_END
 297 
 298 // array allocation without zeroing
 299 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_nozero_C, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 300   JRT_BLOCK;
 301 #ifndef PRODUCT
 302   SharedRuntime::_new_array_ctr++;            // new array requires GC
 303 #endif
 304   assert(check_compiled_frame(current), "incorrect caller");
 305 
 306   // Scavenge and allocate an instance.
 307   oop result;
 308 
 309   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 310   // The oopFactory likes to work with the element type.
 311   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 312   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 313 
 314   // Pass oops back through thread local storage.  Our apparent type to Java
 315   // is that we return an oop, but we can block on exit from this routine and
 316   // a GC can trash the oop in C's return register.  The generated stub will
 317   // fetch the oop from TLS after any possible GC.
 318   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 319   current->set_vm_result(result);
 320   JRT_BLOCK_END;
 321 
 322 
 323   // inform GC that we won't do card marks for initializing writes.
 324   SharedRuntime::on_slowpath_allocation_exit(current);
 325 
 326   oop result = current->vm_result();
 327   if ((len > 0) && (result != nullptr) &&
 328       is_deoptimized_caller_frame(current)) {
 329     // Zero array here if the caller is deoptimized.
 330     const size_t size = TypeArrayKlass::cast(array_type)->oop_size(result);
 331     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 332     const size_t hs = arrayOopDesc::header_size(elem_type);
 333     // Align to next 8 bytes to avoid trashing arrays's length.
 334     const size_t aligned_hs = align_object_offset(hs);
 335     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 336     if (aligned_hs > hs) {
 337       Copy::zero_to_words(obj+hs, aligned_hs-hs);
 338     }
 339     // Optimized zeroing.
 340     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 341   }
 342 
 343 JRT_END
 344 
 345 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 346 
 347 // multianewarray for 2 dimensions
 348 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray2_C, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 349 #ifndef PRODUCT
 350   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 351 #endif
 352   assert(check_compiled_frame(current), "incorrect caller");
 353   assert(elem_type->is_klass(), "not a class");
 354   jint dims[2];
 355   dims[0] = len1;
 356   dims[1] = len2;
 357   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 358   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 359   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 360   current->set_vm_result(obj);
 361 JRT_END
 362 
 363 // multianewarray for 3 dimensions
 364 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray3_C, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 365 #ifndef PRODUCT
 366   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 367 #endif
 368   assert(check_compiled_frame(current), "incorrect caller");
 369   assert(elem_type->is_klass(), "not a class");
 370   jint dims[3];
 371   dims[0] = len1;
 372   dims[1] = len2;
 373   dims[2] = len3;
 374   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 375   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 376   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 377   current->set_vm_result(obj);
 378 JRT_END
 379 
 380 // multianewarray for 4 dimensions
 381 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray4_C, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 382 #ifndef PRODUCT
 383   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 384 #endif
 385   assert(check_compiled_frame(current), "incorrect caller");
 386   assert(elem_type->is_klass(), "not a class");
 387   jint dims[4];
 388   dims[0] = len1;
 389   dims[1] = len2;
 390   dims[2] = len3;
 391   dims[3] = len4;
 392   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 393   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 394   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 395   current->set_vm_result(obj);
 396 JRT_END
 397 
 398 // multianewarray for 5 dimensions
 399 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 400 #ifndef PRODUCT
 401   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 402 #endif
 403   assert(check_compiled_frame(current), "incorrect caller");
 404   assert(elem_type->is_klass(), "not a class");
 405   jint dims[5];
 406   dims[0] = len1;
 407   dims[1] = len2;
 408   dims[2] = len3;
 409   dims[3] = len4;
 410   dims[4] = len5;
 411   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 412   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 413   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 414   current->set_vm_result(obj);
 415 JRT_END
 416 
 417 JRT_ENTRY_PROF(void, OptoRuntime, multianewarrayN_C, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 418   assert(check_compiled_frame(current), "incorrect caller");
 419   assert(elem_type->is_klass(), "not a class");
 420   assert(oop(dims)->is_typeArray(), "not an array");
 421 
 422   ResourceMark rm;
 423   jint len = dims->length();
 424   assert(len > 0, "Dimensions array should contain data");
 425   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 426   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 427                                        c_dims, len);
 428 
 429   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 430   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 431   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 432   current->set_vm_result(obj);
 433 JRT_END
 434 
 435 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notify_C, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 436 
 437   // Very few notify/notifyAll operations find any threads on the waitset, so
 438   // the dominant fast-path is to simply return.
 439   // Relatedly, it's critical that notify/notifyAll be fast in order to
 440   // reduce lock hold times.
 441   if (!SafepointSynchronize::is_synchronizing()) {
 442     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 443       return;
 444     }
 445   }
 446 
 447   // This is the case the fast-path above isn't provisioned to handle.
 448   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 449   // (The fast-path is just a degenerate variant of the slow-path).
 450   // Perform the dreaded state transition and pass control into the slow-path.
 451   JRT_BLOCK;
 452   Handle h_obj(current, obj);
 453   ObjectSynchronizer::notify(h_obj, CHECK);
 454   JRT_BLOCK_END;
 455 JRT_END
 456 
 457 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notifyAll_C, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 458 
 459   if (!SafepointSynchronize::is_synchronizing() ) {
 460     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 461       return;
 462     }
 463   }
 464 
 465   // This is the case the fast-path above isn't provisioned to handle.
 466   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 467   // (The fast-path is just a degenerate variant of the slow-path).
 468   // Perform the dreaded state transition and pass control into the slow-path.
 469   JRT_BLOCK;
 470   Handle h_obj(current, obj);
 471   ObjectSynchronizer::notifyall(h_obj, CHECK);
 472   JRT_BLOCK_END;
 473 JRT_END
 474 
 475 const TypeFunc *OptoRuntime::new_instance_Type() {
 476   // create input type (domain)
 477   const Type **fields = TypeTuple::fields(1);
 478   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 479   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 480 
 481   // create result type (range)
 482   fields = TypeTuple::fields(1);
 483   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 484 
 485   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 486 
 487   return TypeFunc::make(domain, range);
 488 }
 489 
 490 #if INCLUDE_JVMTI
 491 const TypeFunc *OptoRuntime::notify_jvmti_vthread_Type() {
 492   // create input type (domain)
 493   const Type **fields = TypeTuple::fields(2);
 494   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // VirtualThread oop
 495   fields[TypeFunc::Parms+1] = TypeInt::BOOL;        // jboolean
 496   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 497 
 498   // no result type needed
 499   fields = TypeTuple::fields(1);
 500   fields[TypeFunc::Parms+0] = nullptr; // void
 501   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 502 
 503   return TypeFunc::make(domain,range);
 504 }
 505 #endif
 506 
 507 const TypeFunc *OptoRuntime::athrow_Type() {
 508   // create input type (domain)
 509   const Type **fields = TypeTuple::fields(1);
 510   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 511   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 512 
 513   // create result type (range)
 514   fields = TypeTuple::fields(0);
 515 
 516   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 517 
 518   return TypeFunc::make(domain, range);
 519 }
 520 
 521 
 522 const TypeFunc *OptoRuntime::new_array_Type() {
 523   // create input type (domain)
 524   const Type **fields = TypeTuple::fields(2);
 525   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 526   fields[TypeFunc::Parms+1] = TypeInt::INT;       // array size
 527   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 528 
 529   // create result type (range)
 530   fields = TypeTuple::fields(1);
 531   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 532 
 533   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 534 
 535   return TypeFunc::make(domain, range);
 536 }
 537 
 538 const TypeFunc *OptoRuntime::multianewarray_Type(int ndim) {
 539   // create input type (domain)
 540   const int nargs = ndim + 1;
 541   const Type **fields = TypeTuple::fields(nargs);
 542   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 543   for( int i = 1; i < nargs; i++ )
 544     fields[TypeFunc::Parms + i] = TypeInt::INT;       // array size
 545   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+nargs, fields);
 546 
 547   // create result type (range)
 548   fields = TypeTuple::fields(1);
 549   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 550   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 551 
 552   return TypeFunc::make(domain, range);
 553 }
 554 
 555 const TypeFunc *OptoRuntime::multianewarray2_Type() {
 556   return multianewarray_Type(2);
 557 }
 558 
 559 const TypeFunc *OptoRuntime::multianewarray3_Type() {
 560   return multianewarray_Type(3);
 561 }
 562 
 563 const TypeFunc *OptoRuntime::multianewarray4_Type() {
 564   return multianewarray_Type(4);
 565 }
 566 
 567 const TypeFunc *OptoRuntime::multianewarray5_Type() {
 568   return multianewarray_Type(5);
 569 }
 570 
 571 const TypeFunc *OptoRuntime::multianewarrayN_Type() {
 572   // create input type (domain)
 573   const Type **fields = TypeTuple::fields(2);
 574   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 575   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;   // array of dim sizes
 576   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 577 
 578   // create result type (range)
 579   fields = TypeTuple::fields(1);
 580   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 581   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 582 
 583   return TypeFunc::make(domain, range);
 584 }
 585 
 586 const TypeFunc *OptoRuntime::uncommon_trap_Type() {
 587   // create input type (domain)
 588   const Type **fields = TypeTuple::fields(1);
 589   fields[TypeFunc::Parms+0] = TypeInt::INT; // trap_reason (deopt reason and action)
 590   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 591 
 592   // create result type (range)
 593   fields = TypeTuple::fields(0);
 594   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 595 
 596   return TypeFunc::make(domain, range);
 597 }
 598 
 599 //-----------------------------------------------------------------------------
 600 // Monitor Handling
 601 const TypeFunc *OptoRuntime::complete_monitor_enter_Type() {
 602   // create input type (domain)
 603   const Type **fields = TypeTuple::fields(2);
 604   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 605   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // Address of stack location for lock
 606   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 607 
 608   // create result type (range)
 609   fields = TypeTuple::fields(0);
 610 
 611   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
 612 
 613   return TypeFunc::make(domain,range);
 614 }
 615 
 616 
 617 //-----------------------------------------------------------------------------
 618 const TypeFunc *OptoRuntime::complete_monitor_exit_Type() {
 619   // create input type (domain)
 620   const Type **fields = TypeTuple::fields(3);
 621   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 622   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;    // Address of stack location for lock - BasicLock
 623   fields[TypeFunc::Parms+2] = TypeRawPtr::BOTTOM;    // Thread pointer (Self)
 624   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+3, fields);
 625 
 626   // create result type (range)
 627   fields = TypeTuple::fields(0);
 628 
 629   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 630 
 631   return TypeFunc::make(domain, range);
 632 }
 633 
 634 const TypeFunc *OptoRuntime::monitor_notify_Type() {
 635   // create input type (domain)
 636   const Type **fields = TypeTuple::fields(1);
 637   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 638   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 639 
 640   // create result type (range)
 641   fields = TypeTuple::fields(0);
 642   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 643   return TypeFunc::make(domain, range);
 644 }
 645 
 646 const TypeFunc* OptoRuntime::flush_windows_Type() {
 647   // create input type (domain)
 648   const Type** fields = TypeTuple::fields(1);
 649   fields[TypeFunc::Parms+0] = nullptr; // void
 650   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
 651 
 652   // create result type
 653   fields = TypeTuple::fields(1);
 654   fields[TypeFunc::Parms+0] = nullptr; // void
 655   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 656 
 657   return TypeFunc::make(domain, range);
 658 }
 659 
 660 const TypeFunc* OptoRuntime::l2f_Type() {
 661   // create input type (domain)
 662   const Type **fields = TypeTuple::fields(2);
 663   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 664   fields[TypeFunc::Parms+1] = Type::HALF;
 665   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 666 
 667   // create result type (range)
 668   fields = TypeTuple::fields(1);
 669   fields[TypeFunc::Parms+0] = Type::FLOAT;
 670   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 671 
 672   return TypeFunc::make(domain, range);
 673 }
 674 
 675 const TypeFunc* OptoRuntime::modf_Type() {
 676   const Type **fields = TypeTuple::fields(2);
 677   fields[TypeFunc::Parms+0] = Type::FLOAT;
 678   fields[TypeFunc::Parms+1] = Type::FLOAT;
 679   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 680 
 681   // create result type (range)
 682   fields = TypeTuple::fields(1);
 683   fields[TypeFunc::Parms+0] = Type::FLOAT;
 684 
 685   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 686 
 687   return TypeFunc::make(domain, range);
 688 }
 689 
 690 const TypeFunc *OptoRuntime::Math_D_D_Type() {
 691   // create input type (domain)
 692   const Type **fields = TypeTuple::fields(2);
 693   // Symbol* name of class to be loaded
 694   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 695   fields[TypeFunc::Parms+1] = Type::HALF;
 696   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 697 
 698   // create result type (range)
 699   fields = TypeTuple::fields(2);
 700   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 701   fields[TypeFunc::Parms+1] = Type::HALF;
 702   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 703 
 704   return TypeFunc::make(domain, range);
 705 }
 706 
 707 const TypeFunc *OptoRuntime::Math_Vector_Vector_Type(uint num_arg, const TypeVect* in_type, const TypeVect* out_type) {
 708   // create input type (domain)
 709   const Type **fields = TypeTuple::fields(num_arg);
 710   // Symbol* name of class to be loaded
 711   assert(num_arg > 0, "must have at least 1 input");
 712   for (uint i = 0; i < num_arg; i++) {
 713     fields[TypeFunc::Parms+i] = in_type;
 714   }
 715   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+num_arg, fields);
 716 
 717   // create result type (range)
 718   const uint num_ret = 1;
 719   fields = TypeTuple::fields(num_ret);
 720   fields[TypeFunc::Parms+0] = out_type;
 721   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+num_ret, fields);
 722 
 723   return TypeFunc::make(domain, range);
 724 }
 725 
 726 const TypeFunc* OptoRuntime::Math_DD_D_Type() {
 727   const Type **fields = TypeTuple::fields(4);
 728   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 729   fields[TypeFunc::Parms+1] = Type::HALF;
 730   fields[TypeFunc::Parms+2] = Type::DOUBLE;
 731   fields[TypeFunc::Parms+3] = Type::HALF;
 732   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+4, fields);
 733 
 734   // create result type (range)
 735   fields = TypeTuple::fields(2);
 736   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 737   fields[TypeFunc::Parms+1] = Type::HALF;
 738   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 739 
 740   return TypeFunc::make(domain, range);
 741 }
 742 
 743 //-------------- currentTimeMillis, currentTimeNanos, etc
 744 
 745 const TypeFunc* OptoRuntime::void_long_Type() {
 746   // create input type (domain)
 747   const Type **fields = TypeTuple::fields(0);
 748   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
 749 
 750   // create result type (range)
 751   fields = TypeTuple::fields(2);
 752   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 753   fields[TypeFunc::Parms+1] = Type::HALF;
 754   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 755 
 756   return TypeFunc::make(domain, range);
 757 }
 758 
 759 const TypeFunc* OptoRuntime::void_void_Type() {
 760    // create input type (domain)
 761    const Type **fields = TypeTuple::fields(0);
 762    const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
 763 
 764    // create result type (range)
 765    fields = TypeTuple::fields(0);
 766    const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 767    return TypeFunc::make(domain, range);
 768  }
 769 
 770  const TypeFunc* OptoRuntime::jfr_write_checkpoint_Type() {
 771    // create input type (domain)
 772    const Type **fields = TypeTuple::fields(0);
 773    const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
 774 
 775    // create result type (range)
 776    fields = TypeTuple::fields(0);
 777    const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 778    return TypeFunc::make(domain, range);
 779  }
 780 
 781 
 782 // arraycopy stub variations:
 783 enum ArrayCopyType {
 784   ac_fast,                      // void(ptr, ptr, size_t)
 785   ac_checkcast,                 //  int(ptr, ptr, size_t, size_t, ptr)
 786   ac_slow,                      // void(ptr, int, ptr, int, int)
 787   ac_generic                    //  int(ptr, int, ptr, int, int)
 788 };
 789 
 790 static const TypeFunc* make_arraycopy_Type(ArrayCopyType act) {
 791   // create input type (domain)
 792   int num_args      = (act == ac_fast ? 3 : 5);
 793   int num_size_args = (act == ac_fast ? 1 : act == ac_checkcast ? 2 : 0);
 794   int argcnt = num_args;
 795   LP64_ONLY(argcnt += num_size_args); // halfwords for lengths
 796   const Type** fields = TypeTuple::fields(argcnt);
 797   int argp = TypeFunc::Parms;
 798   fields[argp++] = TypePtr::NOTNULL;    // src
 799   if (num_size_args == 0) {
 800     fields[argp++] = TypeInt::INT;      // src_pos
 801   }
 802   fields[argp++] = TypePtr::NOTNULL;    // dest
 803   if (num_size_args == 0) {
 804     fields[argp++] = TypeInt::INT;      // dest_pos
 805     fields[argp++] = TypeInt::INT;      // length
 806   }
 807   while (num_size_args-- > 0) {
 808     fields[argp++] = TypeX_X;               // size in whatevers (size_t)
 809     LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
 810   }
 811   if (act == ac_checkcast) {
 812     fields[argp++] = TypePtr::NOTNULL;  // super_klass
 813   }
 814   assert(argp == TypeFunc::Parms+argcnt, "correct decoding of act");
 815   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 816 
 817   // create result type if needed
 818   int retcnt = (act == ac_checkcast || act == ac_generic ? 1 : 0);
 819   fields = TypeTuple::fields(1);
 820   if (retcnt == 0)
 821     fields[TypeFunc::Parms+0] = nullptr; // void
 822   else
 823     fields[TypeFunc::Parms+0] = TypeInt::INT; // status result, if needed
 824   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+retcnt, fields);
 825   return TypeFunc::make(domain, range);
 826 }
 827 
 828 const TypeFunc* OptoRuntime::fast_arraycopy_Type() {
 829   // This signature is simple:  Two base pointers and a size_t.
 830   return make_arraycopy_Type(ac_fast);
 831 }
 832 
 833 const TypeFunc* OptoRuntime::checkcast_arraycopy_Type() {
 834   // An extension of fast_arraycopy_Type which adds type checking.
 835   return make_arraycopy_Type(ac_checkcast);
 836 }
 837 
 838 const TypeFunc* OptoRuntime::slow_arraycopy_Type() {
 839   // This signature is exactly the same as System.arraycopy.
 840   // There are no intptr_t (int/long) arguments.
 841   return make_arraycopy_Type(ac_slow);
 842 }
 843 
 844 const TypeFunc* OptoRuntime::generic_arraycopy_Type() {
 845   // This signature is like System.arraycopy, except that it returns status.
 846   return make_arraycopy_Type(ac_generic);
 847 }
 848 
 849 
 850 const TypeFunc* OptoRuntime::array_fill_Type() {
 851   const Type** fields;
 852   int argp = TypeFunc::Parms;
 853   // create input type (domain): pointer, int, size_t
 854   fields = TypeTuple::fields(3 LP64_ONLY( + 1));
 855   fields[argp++] = TypePtr::NOTNULL;
 856   fields[argp++] = TypeInt::INT;
 857   fields[argp++] = TypeX_X;               // size in whatevers (size_t)
 858   LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
 859   const TypeTuple *domain = TypeTuple::make(argp, fields);
 860 
 861   // create result type
 862   fields = TypeTuple::fields(1);
 863   fields[TypeFunc::Parms+0] = nullptr; // void
 864   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 865 
 866   return TypeFunc::make(domain, range);
 867 }
 868 
 869 const TypeFunc* OptoRuntime::array_partition_Type() {
 870   // create input type (domain)
 871   int num_args = 7;
 872   int argcnt = num_args;
 873   const Type** fields = TypeTuple::fields(argcnt);
 874   int argp = TypeFunc::Parms;
 875   fields[argp++] = TypePtr::NOTNULL;  // array
 876   fields[argp++] = TypeInt::INT;      // element type
 877   fields[argp++] = TypeInt::INT;      // low
 878   fields[argp++] = TypeInt::INT;      // end
 879   fields[argp++] = TypePtr::NOTNULL;  // pivot_indices (int array)
 880   fields[argp++] = TypeInt::INT;      // indexPivot1
 881   fields[argp++] = TypeInt::INT;      // indexPivot2
 882   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 883   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 884 
 885   // no result type needed
 886   fields = TypeTuple::fields(1);
 887   fields[TypeFunc::Parms+0] = nullptr; // void
 888   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 889   return TypeFunc::make(domain, range);
 890 }
 891 
 892 const TypeFunc* OptoRuntime::array_sort_Type() {
 893   // create input type (domain)
 894   int num_args      = 4;
 895   int argcnt = num_args;
 896   const Type** fields = TypeTuple::fields(argcnt);
 897   int argp = TypeFunc::Parms;
 898   fields[argp++] = TypePtr::NOTNULL;    // array
 899   fields[argp++] = TypeInt::INT;    // element type
 900   fields[argp++] = TypeInt::INT;    // fromIndex
 901   fields[argp++] = TypeInt::INT;    // toIndex
 902   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 903   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 904 
 905   // no result type needed
 906   fields = TypeTuple::fields(1);
 907   fields[TypeFunc::Parms+0] = nullptr; // void
 908   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 909   return TypeFunc::make(domain, range);
 910 }
 911 
 912 // for aescrypt encrypt/decrypt operations, just three pointers returning void (length is constant)
 913 const TypeFunc* OptoRuntime::aescrypt_block_Type() {
 914   // create input type (domain)
 915   int num_args      = 3;
 916   int argcnt = num_args;
 917   const Type** fields = TypeTuple::fields(argcnt);
 918   int argp = TypeFunc::Parms;
 919   fields[argp++] = TypePtr::NOTNULL;    // src
 920   fields[argp++] = TypePtr::NOTNULL;    // dest
 921   fields[argp++] = TypePtr::NOTNULL;    // k array
 922   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 923   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 924 
 925   // no result type needed
 926   fields = TypeTuple::fields(1);
 927   fields[TypeFunc::Parms+0] = nullptr; // void
 928   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 929   return TypeFunc::make(domain, range);
 930 }
 931 
 932 /**
 933  * int updateBytesCRC32(int crc, byte* b, int len)
 934  */
 935 const TypeFunc* OptoRuntime::updateBytesCRC32_Type() {
 936   // create input type (domain)
 937   int num_args      = 3;
 938   int argcnt = num_args;
 939   const Type** fields = TypeTuple::fields(argcnt);
 940   int argp = TypeFunc::Parms;
 941   fields[argp++] = TypeInt::INT;        // crc
 942   fields[argp++] = TypePtr::NOTNULL;    // src
 943   fields[argp++] = TypeInt::INT;        // len
 944   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 945   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 946 
 947   // result type needed
 948   fields = TypeTuple::fields(1);
 949   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
 950   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
 951   return TypeFunc::make(domain, range);
 952 }
 953 
 954 /**
 955  * int updateBytesCRC32C(int crc, byte* buf, int len, int* table)
 956  */
 957 const TypeFunc* OptoRuntime::updateBytesCRC32C_Type() {
 958   // create input type (domain)
 959   int num_args      = 4;
 960   int argcnt = num_args;
 961   const Type** fields = TypeTuple::fields(argcnt);
 962   int argp = TypeFunc::Parms;
 963   fields[argp++] = TypeInt::INT;        // crc
 964   fields[argp++] = TypePtr::NOTNULL;    // buf
 965   fields[argp++] = TypeInt::INT;        // len
 966   fields[argp++] = TypePtr::NOTNULL;    // table
 967   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 968   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 969 
 970   // result type needed
 971   fields = TypeTuple::fields(1);
 972   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
 973   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
 974   return TypeFunc::make(domain, range);
 975 }
 976 
 977 /**
 978 *  int updateBytesAdler32(int adler, bytes* b, int off, int len)
 979 */
 980 const TypeFunc* OptoRuntime::updateBytesAdler32_Type() {
 981   // create input type (domain)
 982   int num_args      = 3;
 983   int argcnt = num_args;
 984   const Type** fields = TypeTuple::fields(argcnt);
 985   int argp = TypeFunc::Parms;
 986   fields[argp++] = TypeInt::INT;        // crc
 987   fields[argp++] = TypePtr::NOTNULL;    // src + offset
 988   fields[argp++] = TypeInt::INT;        // len
 989   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 990   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 991 
 992   // result type needed
 993   fields = TypeTuple::fields(1);
 994   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
 995   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
 996   return TypeFunc::make(domain, range);
 997 }
 998 
 999 // for cipherBlockChaining calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
1000 const TypeFunc* OptoRuntime::cipherBlockChaining_aescrypt_Type() {
1001   // create input type (domain)
1002   int num_args      = 5;
1003   int argcnt = num_args;
1004   const Type** fields = TypeTuple::fields(argcnt);
1005   int argp = TypeFunc::Parms;
1006   fields[argp++] = TypePtr::NOTNULL;    // src
1007   fields[argp++] = TypePtr::NOTNULL;    // dest
1008   fields[argp++] = TypePtr::NOTNULL;    // k array
1009   fields[argp++] = TypePtr::NOTNULL;    // r array
1010   fields[argp++] = TypeInt::INT;        // src len
1011   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1012   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1013 
1014   // returning cipher len (int)
1015   fields = TypeTuple::fields(1);
1016   fields[TypeFunc::Parms+0] = TypeInt::INT;
1017   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1018   return TypeFunc::make(domain, range);
1019 }
1020 
1021 // for electronicCodeBook calls of aescrypt encrypt/decrypt, three pointers and a length, returning int
1022 const TypeFunc* OptoRuntime::electronicCodeBook_aescrypt_Type() {
1023   // create input type (domain)
1024   int num_args = 4;
1025   int argcnt = num_args;
1026   const Type** fields = TypeTuple::fields(argcnt);
1027   int argp = TypeFunc::Parms;
1028   fields[argp++] = TypePtr::NOTNULL;    // src
1029   fields[argp++] = TypePtr::NOTNULL;    // dest
1030   fields[argp++] = TypePtr::NOTNULL;    // k array
1031   fields[argp++] = TypeInt::INT;        // src len
1032   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1033   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1034 
1035   // returning cipher len (int)
1036   fields = TypeTuple::fields(1);
1037   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1038   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1039   return TypeFunc::make(domain, range);
1040 }
1041 
1042 //for counterMode calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
1043 const TypeFunc* OptoRuntime::counterMode_aescrypt_Type() {
1044   // create input type (domain)
1045   int num_args = 7;
1046   int argcnt = num_args;
1047   const Type** fields = TypeTuple::fields(argcnt);
1048   int argp = TypeFunc::Parms;
1049   fields[argp++] = TypePtr::NOTNULL; // src
1050   fields[argp++] = TypePtr::NOTNULL; // dest
1051   fields[argp++] = TypePtr::NOTNULL; // k array
1052   fields[argp++] = TypePtr::NOTNULL; // counter array
1053   fields[argp++] = TypeInt::INT; // src len
1054   fields[argp++] = TypePtr::NOTNULL; // saved_encCounter
1055   fields[argp++] = TypePtr::NOTNULL; // saved used addr
1056   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1057   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1058   // returning cipher len (int)
1059   fields = TypeTuple::fields(1);
1060   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1061   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1062   return TypeFunc::make(domain, range);
1063 }
1064 
1065 //for counterMode calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
1066 const TypeFunc* OptoRuntime::galoisCounterMode_aescrypt_Type() {
1067   // create input type (domain)
1068   int num_args = 8;
1069   int argcnt = num_args;
1070   const Type** fields = TypeTuple::fields(argcnt);
1071   int argp = TypeFunc::Parms;
1072   fields[argp++] = TypePtr::NOTNULL; // byte[] in + inOfs
1073   fields[argp++] = TypeInt::INT;     // int len
1074   fields[argp++] = TypePtr::NOTNULL; // byte[] ct + ctOfs
1075   fields[argp++] = TypePtr::NOTNULL; // byte[] out + outOfs
1076   fields[argp++] = TypePtr::NOTNULL; // byte[] key from AESCrypt obj
1077   fields[argp++] = TypePtr::NOTNULL; // long[] state from GHASH obj
1078   fields[argp++] = TypePtr::NOTNULL; // long[] subkeyHtbl from GHASH obj
1079   fields[argp++] = TypePtr::NOTNULL; // byte[] counter from GCTR obj
1080 
1081   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1082   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1083   // returning cipher len (int)
1084   fields = TypeTuple::fields(1);
1085   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1086   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1087   return TypeFunc::make(domain, range);
1088 }
1089 
1090 /*
1091  * void implCompress(byte[] buf, int ofs)
1092  */
1093 const TypeFunc* OptoRuntime::digestBase_implCompress_Type(bool is_sha3) {
1094   // create input type (domain)
1095   int num_args = is_sha3 ? 3 : 2;
1096   int argcnt = num_args;
1097   const Type** fields = TypeTuple::fields(argcnt);
1098   int argp = TypeFunc::Parms;
1099   fields[argp++] = TypePtr::NOTNULL; // buf
1100   fields[argp++] = TypePtr::NOTNULL; // state
1101   if (is_sha3) fields[argp++] = TypeInt::INT; // block_size
1102   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1103   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1104 
1105   // no result type needed
1106   fields = TypeTuple::fields(1);
1107   fields[TypeFunc::Parms+0] = nullptr; // void
1108   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1109   return TypeFunc::make(domain, range);
1110 }
1111 
1112 /*
1113  * int implCompressMultiBlock(byte[] b, int ofs, int limit)
1114  */
1115 const TypeFunc* OptoRuntime::digestBase_implCompressMB_Type(bool is_sha3) {
1116   // create input type (domain)
1117   int num_args = is_sha3 ? 5 : 4;
1118   int argcnt = num_args;
1119   const Type** fields = TypeTuple::fields(argcnt);
1120   int argp = TypeFunc::Parms;
1121   fields[argp++] = TypePtr::NOTNULL; // buf
1122   fields[argp++] = TypePtr::NOTNULL; // state
1123   if (is_sha3) fields[argp++] = TypeInt::INT; // block_size
1124   fields[argp++] = TypeInt::INT;     // ofs
1125   fields[argp++] = TypeInt::INT;     // limit
1126   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1127   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1128 
1129   // returning ofs (int)
1130   fields = TypeTuple::fields(1);
1131   fields[TypeFunc::Parms+0] = TypeInt::INT; // ofs
1132   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1133   return TypeFunc::make(domain, range);
1134 }
1135 
1136 const TypeFunc* OptoRuntime::multiplyToLen_Type() {
1137   // create input type (domain)
1138   int num_args      = 6;
1139   int argcnt = num_args;
1140   const Type** fields = TypeTuple::fields(argcnt);
1141   int argp = TypeFunc::Parms;
1142   fields[argp++] = TypePtr::NOTNULL;    // x
1143   fields[argp++] = TypeInt::INT;        // xlen
1144   fields[argp++] = TypePtr::NOTNULL;    // y
1145   fields[argp++] = TypeInt::INT;        // ylen
1146   fields[argp++] = TypePtr::NOTNULL;    // z
1147   fields[argp++] = TypeInt::INT;        // zlen
1148   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1149   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1150 
1151   // no result type needed
1152   fields = TypeTuple::fields(1);
1153   fields[TypeFunc::Parms+0] = nullptr;
1154   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1155   return TypeFunc::make(domain, range);
1156 }
1157 
1158 const TypeFunc* OptoRuntime::squareToLen_Type() {
1159   // create input type (domain)
1160   int num_args      = 4;
1161   int argcnt = num_args;
1162   const Type** fields = TypeTuple::fields(argcnt);
1163   int argp = TypeFunc::Parms;
1164   fields[argp++] = TypePtr::NOTNULL;    // x
1165   fields[argp++] = TypeInt::INT;        // len
1166   fields[argp++] = TypePtr::NOTNULL;    // z
1167   fields[argp++] = TypeInt::INT;        // zlen
1168   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1169   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1170 
1171   // no result type needed
1172   fields = TypeTuple::fields(1);
1173   fields[TypeFunc::Parms+0] = nullptr;
1174   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1175   return TypeFunc::make(domain, range);
1176 }
1177 
1178 // for mulAdd calls, 2 pointers and 3 ints, returning int
1179 const TypeFunc* OptoRuntime::mulAdd_Type() {
1180   // create input type (domain)
1181   int num_args      = 5;
1182   int argcnt = num_args;
1183   const Type** fields = TypeTuple::fields(argcnt);
1184   int argp = TypeFunc::Parms;
1185   fields[argp++] = TypePtr::NOTNULL;    // out
1186   fields[argp++] = TypePtr::NOTNULL;    // in
1187   fields[argp++] = TypeInt::INT;        // offset
1188   fields[argp++] = TypeInt::INT;        // len
1189   fields[argp++] = TypeInt::INT;        // k
1190   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1191   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1192 
1193   // returning carry (int)
1194   fields = TypeTuple::fields(1);
1195   fields[TypeFunc::Parms+0] = TypeInt::INT;
1196   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1197   return TypeFunc::make(domain, range);
1198 }
1199 
1200 const TypeFunc* OptoRuntime::montgomeryMultiply_Type() {
1201   // create input type (domain)
1202   int num_args      = 7;
1203   int argcnt = num_args;
1204   const Type** fields = TypeTuple::fields(argcnt);
1205   int argp = TypeFunc::Parms;
1206   fields[argp++] = TypePtr::NOTNULL;    // a
1207   fields[argp++] = TypePtr::NOTNULL;    // b
1208   fields[argp++] = TypePtr::NOTNULL;    // n
1209   fields[argp++] = TypeInt::INT;        // len
1210   fields[argp++] = TypeLong::LONG;      // inv
1211   fields[argp++] = Type::HALF;
1212   fields[argp++] = TypePtr::NOTNULL;    // result
1213   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1214   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1215 
1216   // result type needed
1217   fields = TypeTuple::fields(1);
1218   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1219 
1220   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1221   return TypeFunc::make(domain, range);
1222 }
1223 
1224 const TypeFunc* OptoRuntime::montgomerySquare_Type() {
1225   // create input type (domain)
1226   int num_args      = 6;
1227   int argcnt = num_args;
1228   const Type** fields = TypeTuple::fields(argcnt);
1229   int argp = TypeFunc::Parms;
1230   fields[argp++] = TypePtr::NOTNULL;    // a
1231   fields[argp++] = TypePtr::NOTNULL;    // n
1232   fields[argp++] = TypeInt::INT;        // len
1233   fields[argp++] = TypeLong::LONG;      // inv
1234   fields[argp++] = Type::HALF;
1235   fields[argp++] = TypePtr::NOTNULL;    // result
1236   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1237   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1238 
1239   // result type needed
1240   fields = TypeTuple::fields(1);
1241   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1242 
1243   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1244   return TypeFunc::make(domain, range);
1245 }
1246 
1247 const TypeFunc * OptoRuntime::bigIntegerShift_Type() {
1248   int argcnt = 5;
1249   const Type** fields = TypeTuple::fields(argcnt);
1250   int argp = TypeFunc::Parms;
1251   fields[argp++] = TypePtr::NOTNULL;    // newArr
1252   fields[argp++] = TypePtr::NOTNULL;    // oldArr
1253   fields[argp++] = TypeInt::INT;        // newIdx
1254   fields[argp++] = TypeInt::INT;        // shiftCount
1255   fields[argp++] = TypeInt::INT;        // numIter
1256   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1257   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1258 
1259   // no result type needed
1260   fields = TypeTuple::fields(1);
1261   fields[TypeFunc::Parms + 0] = nullptr;
1262   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1263   return TypeFunc::make(domain, range);
1264 }
1265 
1266 const TypeFunc* OptoRuntime::vectorizedMismatch_Type() {
1267   // create input type (domain)
1268   int num_args = 4;
1269   int argcnt = num_args;
1270   const Type** fields = TypeTuple::fields(argcnt);
1271   int argp = TypeFunc::Parms;
1272   fields[argp++] = TypePtr::NOTNULL;    // obja
1273   fields[argp++] = TypePtr::NOTNULL;    // objb
1274   fields[argp++] = TypeInt::INT;        // length, number of elements
1275   fields[argp++] = TypeInt::INT;        // log2scale, element size
1276   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1277   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1278 
1279   //return mismatch index (int)
1280   fields = TypeTuple::fields(1);
1281   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1282   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1283   return TypeFunc::make(domain, range);
1284 }
1285 
1286 // GHASH block processing
1287 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1288     int argcnt = 4;
1289 
1290     const Type** fields = TypeTuple::fields(argcnt);
1291     int argp = TypeFunc::Parms;
1292     fields[argp++] = TypePtr::NOTNULL;    // state
1293     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1294     fields[argp++] = TypePtr::NOTNULL;    // data
1295     fields[argp++] = TypeInt::INT;        // blocks
1296     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1297     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1298 
1299     // result type needed
1300     fields = TypeTuple::fields(1);
1301     fields[TypeFunc::Parms+0] = nullptr; // void
1302     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1303     return TypeFunc::make(domain, range);
1304 }
1305 
1306 // ChaCha20 Block function
1307 const TypeFunc* OptoRuntime::chacha20Block_Type() {
1308     int argcnt = 2;
1309 
1310     const Type** fields = TypeTuple::fields(argcnt);
1311     int argp = TypeFunc::Parms;
1312     fields[argp++] = TypePtr::NOTNULL;      // state
1313     fields[argp++] = TypePtr::NOTNULL;      // result
1314 
1315     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1316     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1317 
1318     // result type needed
1319     fields = TypeTuple::fields(1);
1320     fields[TypeFunc::Parms + 0] = TypeInt::INT;     // key stream outlen as int
1321     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1322     return TypeFunc::make(domain, range);
1323 }
1324 
1325 // Base64 encode function
1326 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1327   int argcnt = 6;
1328 
1329   const Type** fields = TypeTuple::fields(argcnt);
1330   int argp = TypeFunc::Parms;
1331   fields[argp++] = TypePtr::NOTNULL;    // src array
1332   fields[argp++] = TypeInt::INT;        // offset
1333   fields[argp++] = TypeInt::INT;        // length
1334   fields[argp++] = TypePtr::NOTNULL;    // dest array
1335   fields[argp++] = TypeInt::INT;       // dp
1336   fields[argp++] = TypeInt::BOOL;       // isURL
1337   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1338   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1339 
1340   // result type needed
1341   fields = TypeTuple::fields(1);
1342   fields[TypeFunc::Parms + 0] = nullptr; // void
1343   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1344   return TypeFunc::make(domain, range);
1345 }
1346 // Base64 decode function
1347 const TypeFunc* OptoRuntime::base64_decodeBlock_Type() {
1348   int argcnt = 7;
1349 
1350   const Type** fields = TypeTuple::fields(argcnt);
1351   int argp = TypeFunc::Parms;
1352   fields[argp++] = TypePtr::NOTNULL;    // src array
1353   fields[argp++] = TypeInt::INT;        // src offset
1354   fields[argp++] = TypeInt::INT;        // src length
1355   fields[argp++] = TypePtr::NOTNULL;    // dest array
1356   fields[argp++] = TypeInt::INT;        // dest offset
1357   fields[argp++] = TypeInt::BOOL;       // isURL
1358   fields[argp++] = TypeInt::BOOL;       // isMIME
1359   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1360   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1361 
1362   // result type needed
1363   fields = TypeTuple::fields(1);
1364   fields[TypeFunc::Parms + 0] = TypeInt::INT; // count of bytes written to dst
1365   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1366   return TypeFunc::make(domain, range);
1367 }
1368 
1369 // Poly1305 processMultipleBlocks function
1370 const TypeFunc* OptoRuntime::poly1305_processBlocks_Type() {
1371   int argcnt = 4;
1372 
1373   const Type** fields = TypeTuple::fields(argcnt);
1374   int argp = TypeFunc::Parms;
1375   fields[argp++] = TypePtr::NOTNULL;    // input array
1376   fields[argp++] = TypeInt::INT;        // input length
1377   fields[argp++] = TypePtr::NOTNULL;    // accumulator array
1378   fields[argp++] = TypePtr::NOTNULL;    // r array
1379   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1380   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1381 
1382   // result type needed
1383   fields = TypeTuple::fields(1);
1384   fields[TypeFunc::Parms + 0] = nullptr; // void
1385   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1386   return TypeFunc::make(domain, range);
1387 }
1388 
1389 //------------- Interpreter state access for on stack replacement
1390 const TypeFunc* OptoRuntime::osr_end_Type() {
1391   // create input type (domain)
1392   const Type **fields = TypeTuple::fields(1);
1393   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1394   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1395 
1396   // create result type
1397   fields = TypeTuple::fields(1);
1398   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1399   fields[TypeFunc::Parms+0] = nullptr; // void
1400   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1401   return TypeFunc::make(domain, range);
1402 }
1403 
1404 //-------------------------------------------------------------------------------------
1405 // register policy
1406 
1407 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1408   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1409   switch (register_save_policy[reg]) {
1410     case 'C': return false; //SOC
1411     case 'E': return true ; //SOE
1412     case 'N': return false; //NS
1413     case 'A': return false; //AS
1414   }
1415   ShouldNotReachHere();
1416   return false;
1417 }
1418 
1419 //-----------------------------------------------------------------------
1420 // Exceptions
1421 //
1422 
1423 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1424 
1425 // The method is an entry that is always called by a C++ method not
1426 // directly from compiled code. Compiled code will call the C++ method following.
1427 // We can't allow async exception to be installed during  exception processing.
1428 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1429   // The frame we rethrow the exception to might not have been processed by the GC yet.
1430   // The stack watermark barrier takes care of detecting that and ensuring the frame
1431   // has updated oops.
1432   StackWatermarkSet::after_unwind(current);
1433 
1434   // Do not confuse exception_oop with pending_exception. The exception_oop
1435   // is only used to pass arguments into the method. Not for general
1436   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1437   // the runtime stubs checks this on exit.
1438   assert(current->exception_oop() != nullptr, "exception oop is found");
1439   address handler_address = nullptr;
1440 
1441   Handle exception(current, current->exception_oop());
1442   address pc = current->exception_pc();
1443 
1444   // Clear out the exception oop and pc since looking up an
1445   // exception handler can cause class loading, which might throw an
1446   // exception and those fields are expected to be clear during
1447   // normal bytecode execution.
1448   current->clear_exception_oop_and_pc();
1449 
1450   LogTarget(Info, exceptions) lt;
1451   if (lt.is_enabled()) {
1452     ResourceMark rm;
1453     LogStream ls(lt);
1454     trace_exception(&ls, exception(), pc, "");
1455   }
1456 
1457   // for AbortVMOnException flag
1458   Exceptions::debug_check_abort(exception);
1459 
1460 #ifdef ASSERT
1461   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1462     // should throw an exception here
1463     ShouldNotReachHere();
1464   }
1465 #endif
1466 
1467   // new exception handling: this method is entered only from adapters
1468   // exceptions from compiled java methods are handled in compiled code
1469   // using rethrow node
1470 
1471   nm = CodeCache::find_nmethod(pc);
1472   assert(nm != nullptr, "No NMethod found");
1473   if (nm->is_native_method()) {
1474     fatal("Native method should not have path to exception handling");
1475   } else {
1476     // we are switching to old paradigm: search for exception handler in caller_frame
1477     // instead in exception handler of caller_frame.sender()
1478 
1479     if (JvmtiExport::can_post_on_exceptions()) {
1480       // "Full-speed catching" is not necessary here,
1481       // since we're notifying the VM on every catch.
1482       // Force deoptimization and the rest of the lookup
1483       // will be fine.
1484       deoptimize_caller_frame(current);
1485     }
1486 
1487     // Check the stack guard pages.  If enabled, look for handler in this frame;
1488     // otherwise, forcibly unwind the frame.
1489     //
1490     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1491     bool force_unwind = !current->stack_overflow_state()->reguard_stack();
1492     bool deopting = false;
1493     if (nm->is_deopt_pc(pc)) {
1494       deopting = true;
1495       RegisterMap map(current,
1496                       RegisterMap::UpdateMap::skip,
1497                       RegisterMap::ProcessFrames::include,
1498                       RegisterMap::WalkContinuation::skip);
1499       frame deoptee = current->last_frame().sender(&map);
1500       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1501       // Adjust the pc back to the original throwing pc
1502       pc = deoptee.pc();
1503     }
1504 
1505     // If we are forcing an unwind because of stack overflow then deopt is
1506     // irrelevant since we are throwing the frame away anyway.
1507 
1508     if (deopting && !force_unwind) {
1509       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1510     } else {
1511 
1512       handler_address =
1513         force_unwind ? nullptr : nm->handler_for_exception_and_pc(exception, pc);
1514 
1515       if (handler_address == nullptr) {
1516         bool recursive_exception = false;
1517         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1518         assert (handler_address != nullptr, "must have compiled handler");
1519         // Update the exception cache only when the unwind was not forced
1520         // and there didn't happen another exception during the computation of the
1521         // compiled exception handler. Checking for exception oop equality is not
1522         // sufficient because some exceptions are pre-allocated and reused.
1523         if (!force_unwind && !recursive_exception) {
1524           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1525         }
1526       } else {
1527 #ifdef ASSERT
1528         bool recursive_exception = false;
1529         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1530         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1531                  p2i(handler_address), p2i(computed_address));
1532 #endif
1533       }
1534     }
1535 
1536     current->set_exception_pc(pc);
1537     current->set_exception_handler_pc(handler_address);
1538 
1539     // Check if the exception PC is a MethodHandle call site.
1540     current->set_is_method_handle_return(nm->is_method_handle_return(pc));
1541   }
1542 
1543   // Restore correct return pc.  Was saved above.
1544   current->set_exception_oop(exception());
1545   return handler_address;
1546 
1547 JRT_END
1548 
1549 // We are entering here from exception_blob
1550 // If there is a compiled exception handler in this method, we will continue there;
1551 // otherwise we will unwind the stack and continue at the caller of top frame method
1552 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1553 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1554 // we looked up the handler for has been deoptimized in the meantime. If it has been
1555 // we must not use the handler and instead return the deopt blob.
1556 address OptoRuntime::handle_exception_C(JavaThread* current) {
1557 //
1558 // We are in Java not VM and in debug mode we have a NoHandleMark
1559 //
1560 #ifndef PRODUCT
1561   SharedRuntime::_find_handler_ctr++;          // find exception handler
1562 #endif
1563   debug_only(NoHandleMark __hm;)
1564   nmethod* nm = nullptr;
1565   address handler_address = nullptr;
1566   {
1567     // Enter the VM
1568 
1569     ResetNoHandleMark rnhm;
1570     handler_address = handle_exception_C_helper(current, nm);
1571   }
1572 
1573   // Back in java: Use no oops, DON'T safepoint
1574 
1575   // Now check to see if the handler we are returning is in a now
1576   // deoptimized frame
1577 
1578   if (nm != nullptr) {
1579     RegisterMap map(current,
1580                     RegisterMap::UpdateMap::skip,
1581                     RegisterMap::ProcessFrames::skip,
1582                     RegisterMap::WalkContinuation::skip);
1583     frame caller = current->last_frame().sender(&map);
1584 #ifdef ASSERT
1585     assert(caller.is_compiled_frame(), "must be");
1586 #endif // ASSERT
1587     if (caller.is_deoptimized_frame()) {
1588       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1589     }
1590   }
1591   return handler_address;
1592 }
1593 
1594 //------------------------------rethrow----------------------------------------
1595 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1596 // is either throwing or rethrowing an exception.  The callee-save registers
1597 // have been restored, synchronized objects have been unlocked and the callee
1598 // stack frame has been removed.  The return address was passed in.
1599 // Exception oop is passed as the 1st argument.  This routine is then called
1600 // from the stub.  On exit, we know where to jump in the caller's code.
1601 // After this C code exits, the stub will pop his frame and end in a jump
1602 // (instead of a return).  We enter the caller's default handler.
1603 //
1604 // This must be JRT_LEAF:
1605 //     - caller will not change its state as we cannot block on exit,
1606 //       therefore raw_exception_handler_for_return_address is all it takes
1607 //       to handle deoptimized blobs
1608 //
1609 // However, there needs to be a safepoint check in the middle!  So compiled
1610 // safepoints are completely watertight.
1611 //
1612 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1613 //
1614 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1615 //
1616 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1617   // ret_pc will have been loaded from the stack, so for AArch64 will be signed.
1618   AARCH64_PORT_ONLY(ret_pc = pauth_strip_verifiable(ret_pc));
1619 
1620 #ifndef PRODUCT
1621   SharedRuntime::_rethrow_ctr++;               // count rethrows
1622 #endif
1623   assert (exception != nullptr, "should have thrown a NullPointerException");
1624 #ifdef ASSERT
1625   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1626     // should throw an exception here
1627     ShouldNotReachHere();
1628   }
1629 #endif
1630 
1631   thread->set_vm_result(exception);
1632   // Frame not compiled (handles deoptimization blob)
1633   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1634 }
1635 
1636 
1637 const TypeFunc *OptoRuntime::rethrow_Type() {
1638   // create input type (domain)
1639   const Type **fields = TypeTuple::fields(1);
1640   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1641   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1642 
1643   // create result type (range)
1644   fields = TypeTuple::fields(1);
1645   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1646   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1647 
1648   return TypeFunc::make(domain, range);
1649 }
1650 
1651 
1652 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1653   // Deoptimize the caller before continuing, as the compiled
1654   // exception handler table may not be valid.
1655   if (!StressCompiledExceptionHandlers && doit) {
1656     deoptimize_caller_frame(thread);
1657   }
1658 }
1659 
1660 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1661   // Called from within the owner thread, so no need for safepoint
1662   RegisterMap reg_map(thread,
1663                       RegisterMap::UpdateMap::include,
1664                       RegisterMap::ProcessFrames::include,
1665                       RegisterMap::WalkContinuation::skip);
1666   frame stub_frame = thread->last_frame();
1667   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1668   frame caller_frame = stub_frame.sender(&reg_map);
1669 
1670   // Deoptimize the caller frame.
1671   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1672 }
1673 
1674 
1675 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1676   // Called from within the owner thread, so no need for safepoint
1677   RegisterMap reg_map(thread,
1678                       RegisterMap::UpdateMap::include,
1679                       RegisterMap::ProcessFrames::include,
1680                       RegisterMap::WalkContinuation::skip);
1681   frame stub_frame = thread->last_frame();
1682   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1683   frame caller_frame = stub_frame.sender(&reg_map);
1684   return caller_frame.is_deoptimized_frame();
1685 }
1686 
1687 
1688 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1689   // create input type (domain)
1690   const Type **fields = TypeTuple::fields(1);
1691   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1692   // // The JavaThread* is passed to each routine as the last argument
1693   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1694   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1695 
1696   // create result type (range)
1697   fields = TypeTuple::fields(0);
1698 
1699   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1700 
1701   return TypeFunc::make(domain,range);
1702 }
1703 
1704 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
1705   // create input type (domain)
1706   const Type** fields = TypeTuple::fields(1);
1707   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
1708   // // The JavaThread* is passed to each routine as the last argument
1709   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1710   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1711 
1712   // create result type (range)
1713   fields = TypeTuple::fields(0);
1714   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1715   return TypeFunc::make(domain,range);
1716 }
1717 
1718 #if INCLUDE_JFR
1719 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1720   // create input type (domain)
1721   const Type **fields = TypeTuple::fields(1);
1722   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1723   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1724 
1725   // create result type (range)
1726   fields = TypeTuple::fields(0);
1727 
1728   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1729 
1730   return TypeFunc::make(domain,range);
1731 }
1732 #endif
1733 
1734 //-----------------------------------------------------------------------------
1735 // Dtrace support.  entry and exit probes have the same signature
1736 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1737   // create input type (domain)
1738   const Type **fields = TypeTuple::fields(2);
1739   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1740   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1741   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1742 
1743   // create result type (range)
1744   fields = TypeTuple::fields(0);
1745 
1746   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1747 
1748   return TypeFunc::make(domain,range);
1749 }
1750 
1751 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1752   // create input type (domain)
1753   const Type **fields = TypeTuple::fields(2);
1754   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1755   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1756 
1757   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1758 
1759   // create result type (range)
1760   fields = TypeTuple::fields(0);
1761 
1762   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1763 
1764   return TypeFunc::make(domain,range);
1765 }
1766 
1767 
1768 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1769   assert(oopDesc::is_oop(obj), "must be a valid oop");
1770   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1771   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1772 JRT_END
1773 
1774 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier, OptoRuntime::class_init_barrier(Klass* k, JavaThread* current))
1775   InstanceKlass* ik = InstanceKlass::cast(k);
1776   if (ik->should_be_initialized()) {
1777     ik->initialize(CHECK);
1778   } else if (UsePerfData) {
1779     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
1780   }
1781 JRT_END
1782 
1783 //-----------------------------------------------------------------------------
1784 
1785 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1786 
1787 //
1788 // dump the collected NamedCounters.
1789 //
1790 void OptoRuntime::print_named_counters() {
1791   int total_lock_count = 0;
1792   int eliminated_lock_count = 0;
1793 
1794   NamedCounter* c = _named_counters;
1795   while (c) {
1796     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1797       int count = c->count();
1798       if (count > 0) {
1799         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1800         if (Verbose) {
1801           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1802         }
1803         total_lock_count += count;
1804         if (eliminated) {
1805           eliminated_lock_count += count;
1806         }
1807       }
1808 #if INCLUDE_RTM_OPT
1809     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1810       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1811       if (rlc->nonzero()) {
1812         tty->print_cr("%s", c->name());
1813         rlc->print_on(tty);
1814       }
1815 #endif
1816     }
1817     c = c->next();
1818   }
1819   if (total_lock_count > 0) {
1820     tty->print_cr("dynamic locks: %d", total_lock_count);
1821     if (eliminated_lock_count) {
1822       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1823                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1824     }
1825   }
1826 }
1827 
1828 //
1829 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1830 //  name which consists of method@line for the inlining tree.
1831 //
1832 
1833 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1834   int max_depth = youngest_jvms->depth();
1835 
1836   // Visit scopes from youngest to oldest.
1837   bool first = true;
1838   stringStream st;
1839   for (int depth = max_depth; depth >= 1; depth--) {
1840     JVMState* jvms = youngest_jvms->of_depth(depth);
1841     ciMethod* m = jvms->has_method() ? jvms->method() : nullptr;
1842     if (!first) {
1843       st.print(" ");
1844     } else {
1845       first = false;
1846     }
1847     int bci = jvms->bci();
1848     if (bci < 0) bci = 0;
1849     if (m != nullptr) {
1850       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1851     } else {
1852       st.print("no method");
1853     }
1854     st.print("@%d", bci);
1855     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1856   }
1857   NamedCounter* c;
1858   if (tag == NamedCounter::RTMLockingCounter) {
1859     c = new RTMLockingNamedCounter(st.freeze());
1860   } else {
1861     c = new NamedCounter(st.freeze(), tag);
1862   }
1863 
1864   // atomically add the new counter to the head of the list.  We only
1865   // add counters so this is safe.
1866   NamedCounter* head;
1867   do {
1868     c->set_next(nullptr);
1869     head = _named_counters;
1870     c->set_next(head);
1871   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1872   return c;
1873 }
1874 
1875 int trace_exception_counter = 0;
1876 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1877   trace_exception_counter++;
1878   stringStream tempst;
1879 
1880   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1881   exception_oop->print_value_on(&tempst);
1882   tempst.print(" in ");
1883   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1884   if (blob->is_compiled()) {
1885     CompiledMethod* cm = blob->as_compiled_method_or_null();
1886     cm->method()->print_value_on(&tempst);
1887   } else if (blob->is_runtime_stub()) {
1888     tempst.print("<runtime-stub>");
1889   } else {
1890     tempst.print("<unknown>");
1891   }
1892   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1893   tempst.print("]");
1894 
1895   st->print_raw_cr(tempst.freeze());
1896 }
1897 
1898 #define DO_COUNTERS2(macro2, macro1) \
1899   macro2(OptoRuntime, new_instance_C) \
1900   macro2(OptoRuntime, new_array_C) \
1901   macro2(OptoRuntime, new_array_nozero_C) \
1902   macro2(OptoRuntime, multianewarray2_C) \
1903   macro2(OptoRuntime, multianewarray3_C) \
1904   macro2(OptoRuntime, multianewarray4_C) \
1905   macro2(OptoRuntime, multianewarrayN_C) \
1906   macro2(OptoRuntime, monitor_notify_C) \
1907   macro2(OptoRuntime, monitor_notifyAll_C) \
1908   macro2(OptoRuntime, handle_exception_C_helper) \
1909   macro2(OptoRuntime, register_finalizer) \
1910   macro2(OptoRuntime, class_init_barrier) \
1911   macro1(OptoRuntime, class_init_barrier_redundant)
1912 
1913 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
1914   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1915   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1916 
1917 #define INIT_COUNTER_CNT(sub, name) \
1918   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1919 
1920 void OptoRuntime::init_counters() {
1921   assert(CompilerConfig::is_c2_enabled(), "");
1922 
1923   if (UsePerfData) {
1924     EXCEPTION_MARK;
1925 
1926     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
1927 
1928     if (HAS_PENDING_EXCEPTION) {
1929       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
1930     }
1931   }
1932 }
1933 #undef INIT_COUNTER_TIME_AND_CNT
1934 #undef INIT_COUNTER_CNT
1935 
1936 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
1937   jlong count = _perf_##sub##_##name##_count->get_value(); \
1938   if (count > 0) { \
1939     st->print_cr("  %-30s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
1940                  _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
1941                  _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
1942                  count); \
1943   }}
1944 
1945 #define PRINT_COUNTER_CNT(sub, name) { \
1946   jlong count = _perf_##sub##_##name##_count->get_value(); \
1947   if (count > 0) { \
1948     st->print_cr("  %-30s = %5ld events", #name, count); \
1949   }}
1950 
1951 void OptoRuntime::print_counters_on(outputStream* st) {
1952   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
1953     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
1954   } else {
1955     st->print_cr("  OptoRuntime: no info (%s is disabled)",
1956                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1957   }
1958 }
1959 
1960 #undef PRINT_COUNTER_TIME_AND_CNT
1961 #undef PRINT_COUNTER_CNT
1962 #undef DO_COUNTERS2