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