1 /*
   2  * Copyright (c) 1998, 2025, 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 "classfile/vmClasses.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "code/aotCodeCache.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "code/pcDesc.hpp"
  32 #include "code/scopeDesc.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "compiler/compilationMemoryStatistic.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/oopMap.hpp"
  37 #include "gc/g1/g1HeapRegion.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/klass.inline.hpp"
  49 #include "oops/objArrayKlass.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/atomicAccess.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/mountUnmountDisabler.hpp"
  71 #include "runtime/sharedRuntime.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/stackWatermarkSet.hpp"
  74 #include "runtime/synchronizer.hpp"
  75 #include "runtime/threadWXSetters.inline.hpp"
  76 #include "runtime/vframe.hpp"
  77 #include "runtime/vframe_hp.hpp"
  78 #include "runtime/vframeArray.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 //  Universe::heap()->collect();
  88 //
  89 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  90 
  91 
  92 #define C2_BLOB_FIELD_DEFINE(name, type) \
  93   type* OptoRuntime:: BLOB_FIELD_NAME(name)  = nullptr;
  94 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
  95 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
  96   address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
  97 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE)
  98 #undef C2_BLOB_FIELD_DEFINE
  99 #undef C2_STUB_FIELD_DEFINE
 100 
 101 // This should be called in an assertion at the start of OptoRuntime routines
 102 // which are entered from compiled code (all of them)
 103 #ifdef ASSERT
 104 static bool check_compiled_frame(JavaThread* thread) {
 105   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 106   RegisterMap map(thread,
 107                   RegisterMap::UpdateMap::skip,
 108                   RegisterMap::ProcessFrames::include,
 109                   RegisterMap::WalkContinuation::skip);
 110   frame caller = thread->last_frame().sender(&map);
 111   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 112   return true;
 113 }
 114 #endif // ASSERT
 115 
 116 /*
 117 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 118   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 119   if (var == nullptr) { return false; }
 120 */
 121 
 122 #define GEN_C2_BLOB(name, type)                    \
 123   BLOB_FIELD_NAME(name) =                       \
 124     generate_ ## name ## _blob();                  \
 125   if (BLOB_FIELD_NAME(name) == nullptr) { return false; }
 126 
 127 // a few helper macros to conjure up generate_stub call arguments
 128 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
 129 #define C2_STUB_TYPEFUNC(name) name ## _Type
 130 #define C2_STUB_C_FUNC(name) CAST_FROM_FN_PTR(address, name ## _C)
 131 #define C2_STUB_ID(name) StubId:: JOIN3(c2, name, id)
 132 #define C2_STUB_NAME(name) stub_name(C2_STUB_ID(name))
 133 
 134 // Almost all the C functions targeted from the generated stubs are
 135 // implemented locally to OptoRuntime with names that can be generated
 136 // from the stub name by appending suffix '_C'. However, in two cases
 137 // a common target method also needs to be called from shared runtime
 138 // stubs. In these two cases the opto stubs rely on method
 139 // imlementations defined in class SharedRuntime. The following
 140 // defines temporarily rebind the generated names to reference the
 141 // relevant implementations.
 142 
 143 #define GEN_C2_STUB(name, fancy_jump, pass_tls, pass_retpc  )         \
 144   C2_STUB_FIELD_NAME(name) =                                          \
 145     generate_stub(env,                                                \
 146                   C2_STUB_TYPEFUNC(name),                             \
 147                   C2_STUB_C_FUNC(name),                               \
 148                   C2_STUB_NAME(name),                                 \
 149                   C2_STUB_ID(name),                                   \
 150                   fancy_jump,                                         \
 151                   pass_tls,                                           \
 152                   pass_retpc);                                        \
 153   if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; }          \
 154 
 155 bool OptoRuntime::generate(ciEnv* env) {
 156 
 157   C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB)
 158   // disallow any further c2 stub generation
 159   AOTCodeCache::set_c2_stubs_complete();
 160   return true;
 161 }
 162 
 163 #undef GEN_C2_BLOB
 164 
 165 #undef C2_STUB_FIELD_NAME
 166 #undef C2_STUB_TYPEFUNC
 167 #undef C2_STUB_C_FUNC
 168 #undef C2_STUB_NAME
 169 #undef GEN_C2_STUB
 170 
 171 // #undef gen
 172 
 173 const TypeFunc* OptoRuntime::_new_instance_Type                   = nullptr;
 174 const TypeFunc* OptoRuntime::_new_array_Type                      = nullptr;
 175 const TypeFunc* OptoRuntime::_multianewarray2_Type                = nullptr;
 176 const TypeFunc* OptoRuntime::_multianewarray3_Type                = nullptr;
 177 const TypeFunc* OptoRuntime::_multianewarray4_Type                = nullptr;
 178 const TypeFunc* OptoRuntime::_multianewarray5_Type                = nullptr;
 179 const TypeFunc* OptoRuntime::_multianewarrayN_Type                = nullptr;
 180 const TypeFunc* OptoRuntime::_complete_monitor_enter_Type         = nullptr;
 181 const TypeFunc* OptoRuntime::_complete_monitor_exit_Type          = nullptr;
 182 const TypeFunc* OptoRuntime::_monitor_notify_Type                 = nullptr;
 183 const TypeFunc* OptoRuntime::_uncommon_trap_Type                  = nullptr;
 184 const TypeFunc* OptoRuntime::_athrow_Type                         = nullptr;
 185 const TypeFunc* OptoRuntime::_rethrow_Type                        = nullptr;
 186 const TypeFunc* OptoRuntime::_Math_D_D_Type                       = nullptr;
 187 const TypeFunc* OptoRuntime::_Math_DD_D_Type                      = nullptr;
 188 const TypeFunc* OptoRuntime::_modf_Type                           = nullptr;
 189 const TypeFunc* OptoRuntime::_l2f_Type                            = nullptr;
 190 const TypeFunc* OptoRuntime::_void_long_Type                      = nullptr;
 191 const TypeFunc* OptoRuntime::_void_void_Type                      = nullptr;
 192 const TypeFunc* OptoRuntime::_jfr_write_checkpoint_Type           = nullptr;
 193 const TypeFunc* OptoRuntime::_flush_windows_Type                  = nullptr;
 194 const TypeFunc* OptoRuntime::_fast_arraycopy_Type                 = nullptr;
 195 const TypeFunc* OptoRuntime::_checkcast_arraycopy_Type            = nullptr;
 196 const TypeFunc* OptoRuntime::_generic_arraycopy_Type              = nullptr;
 197 const TypeFunc* OptoRuntime::_slow_arraycopy_Type                 = nullptr;
 198 const TypeFunc* OptoRuntime::_unsafe_setmemory_Type               = nullptr;
 199 const TypeFunc* OptoRuntime::_array_fill_Type                     = nullptr;
 200 const TypeFunc* OptoRuntime::_array_sort_Type                     = nullptr;
 201 const TypeFunc* OptoRuntime::_array_partition_Type                = nullptr;
 202 const TypeFunc* OptoRuntime::_aescrypt_block_Type                 = nullptr;
 203 const TypeFunc* OptoRuntime::_cipherBlockChaining_aescrypt_Type   = nullptr;
 204 const TypeFunc* OptoRuntime::_electronicCodeBook_aescrypt_Type    = nullptr;
 205 const TypeFunc* OptoRuntime::_counterMode_aescrypt_Type           = nullptr;
 206 const TypeFunc* OptoRuntime::_galoisCounterMode_aescrypt_Type     = nullptr;
 207 const TypeFunc* OptoRuntime::_digestBase_implCompress_with_sha3_Type      = nullptr;
 208 const TypeFunc* OptoRuntime::_digestBase_implCompress_without_sha3_Type   = nullptr;
 209 const TypeFunc* OptoRuntime::_digestBase_implCompressMB_with_sha3_Type    = nullptr;
 210 const TypeFunc* OptoRuntime::_digestBase_implCompressMB_without_sha3_Type = nullptr;
 211 const TypeFunc* OptoRuntime::_double_keccak_Type                  = nullptr;
 212 const TypeFunc* OptoRuntime::_multiplyToLen_Type                  = nullptr;
 213 const TypeFunc* OptoRuntime::_montgomeryMultiply_Type             = nullptr;
 214 const TypeFunc* OptoRuntime::_montgomerySquare_Type               = nullptr;
 215 const TypeFunc* OptoRuntime::_squareToLen_Type                    = nullptr;
 216 const TypeFunc* OptoRuntime::_mulAdd_Type                         = nullptr;
 217 const TypeFunc* OptoRuntime::_bigIntegerShift_Type                = nullptr;
 218 const TypeFunc* OptoRuntime::_vectorizedMismatch_Type             = nullptr;
 219 const TypeFunc* OptoRuntime::_ghash_processBlocks_Type            = nullptr;
 220 const TypeFunc* OptoRuntime::_chacha20Block_Type                  = nullptr;
 221 const TypeFunc* OptoRuntime::_kyberNtt_Type                       = nullptr;
 222 const TypeFunc* OptoRuntime::_kyberInverseNtt_Type                = nullptr;
 223 const TypeFunc* OptoRuntime::_kyberNttMult_Type                   = nullptr;
 224 const TypeFunc* OptoRuntime::_kyberAddPoly_2_Type                 = nullptr;
 225 const TypeFunc* OptoRuntime::_kyberAddPoly_3_Type                 = nullptr;
 226 const TypeFunc* OptoRuntime::_kyber12To16_Type                    = nullptr;
 227 const TypeFunc* OptoRuntime::_kyberBarrettReduce_Type             = nullptr;
 228 const TypeFunc* OptoRuntime::_dilithiumAlmostNtt_Type             = nullptr;
 229 const TypeFunc* OptoRuntime::_dilithiumAlmostInverseNtt_Type      = nullptr;
 230 const TypeFunc* OptoRuntime::_dilithiumNttMult_Type               = nullptr;
 231 const TypeFunc* OptoRuntime::_dilithiumMontMulByConstant_Type     = nullptr;
 232 const TypeFunc* OptoRuntime::_dilithiumDecomposePoly_Type         = nullptr;
 233 const TypeFunc* OptoRuntime::_base64_encodeBlock_Type             = nullptr;
 234 const TypeFunc* OptoRuntime::_base64_decodeBlock_Type             = nullptr;
 235 const TypeFunc* OptoRuntime::_string_IndexOf_Type                 = nullptr;
 236 const TypeFunc* OptoRuntime::_poly1305_processBlocks_Type         = nullptr;
 237 const TypeFunc* OptoRuntime::_intpoly_montgomeryMult_P256_Type    = nullptr;
 238 const TypeFunc* OptoRuntime::_intpoly_assign_Type                 = nullptr;
 239 const TypeFunc* OptoRuntime::_updateBytesCRC32_Type               = nullptr;
 240 const TypeFunc* OptoRuntime::_updateBytesCRC32C_Type              = nullptr;
 241 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type             = nullptr;
 242 const TypeFunc* OptoRuntime::_osr_end_Type                        = nullptr;
 243 const TypeFunc* OptoRuntime::_register_finalizer_Type             = nullptr;
 244 const TypeFunc* OptoRuntime::_vthread_transition_Type             = nullptr;
 245 #if INCLUDE_JFR
 246 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type          = nullptr;
 247 #endif // INCLUDE_JFR
 248 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type       = nullptr;
 249 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type            = nullptr;
 250 
 251 // Helper method to do generation of RunTimeStub's
 252 address OptoRuntime::generate_stub(ciEnv* env,
 253                                    TypeFunc_generator gen, address C_function,
 254                                    const char *name, StubId stub_id,
 255                                    int is_fancy_jump, bool pass_tls,
 256                                    bool return_pc) {
 257 
 258   // Matching the default directive, we currently have no method to match.
 259   CompilerDirectiveMatcher default_directive(CompileBroker::compiler(CompLevel_full_optimization));
 260   CompilationMemoryStatisticMark cmsm(default_directive.directive_set());
 261   ResourceMark rm;
 262   Compile C(env, gen, C_function, name, stub_id, is_fancy_jump, pass_tls, return_pc, default_directive.directive_set());
 263   return  C.stub_entry_point();
 264 }
 265 
 266 const char* OptoRuntime::stub_name(address entry) {
 267 #ifndef PRODUCT
 268   CodeBlob* cb = CodeCache::find_blob(entry);
 269   RuntimeStub* rs =(RuntimeStub *)cb;
 270   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 271   return rs->name();
 272 #else
 273   // Fast implementation for product mode (maybe it should be inlined too)
 274   return "runtime stub";
 275 #endif
 276 }
 277 
 278 // local methods passed as arguments to stub generator that forward
 279 // control to corresponding JRT methods of SharedRuntime
 280 
 281 void OptoRuntime::slow_arraycopy_C(oopDesc* src,  jint src_pos,
 282                                    oopDesc* dest, jint dest_pos,
 283                                    jint length, JavaThread* thread) {
 284   SharedRuntime::slow_arraycopy_C(src,  src_pos, dest, dest_pos, length, thread);
 285 }
 286 
 287 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
 288   SharedRuntime::complete_monitor_locking_C(obj, lock, current);
 289 }
 290 
 291 
 292 //=============================================================================
 293 // Opto compiler runtime routines
 294 //=============================================================================
 295 
 296 
 297 //=============================allocation======================================
 298 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 299 // and try allocation again.
 300 
 301 // object allocation
 302 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 303   JRT_BLOCK;
 304 #ifndef PRODUCT
 305   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 306 #endif
 307   assert(check_compiled_frame(current), "incorrect caller");
 308 
 309   // These checks are cheap to make and support reflective allocation.
 310   int lh = klass->layout_helper();
 311   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 312     Handle holder(current, klass->klass_holder()); // keep the klass alive
 313     klass->check_valid_for_instantiation(false, THREAD);
 314     if (!HAS_PENDING_EXCEPTION) {
 315       InstanceKlass::cast(klass)->initialize(THREAD);
 316     }
 317   }
 318 
 319   if (!HAS_PENDING_EXCEPTION) {
 320     // Scavenge and allocate an instance.
 321     Handle holder(current, klass->klass_holder()); // keep the klass alive
 322     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 323     current->set_vm_result_oop(result);
 324 
 325     // Pass oops back through thread local storage.  Our apparent type to Java
 326     // is that we return an oop, but we can block on exit from this routine and
 327     // a GC can trash the oop in C's return register.  The generated stub will
 328     // fetch the oop from TLS after any possible GC.
 329   }
 330 
 331   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 332   JRT_BLOCK_END;
 333 
 334   // inform GC that we won't do card marks for initializing writes.
 335   SharedRuntime::on_slowpath_allocation_exit(current);
 336 JRT_END
 337 
 338 
 339 // array allocation
 340 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 341   JRT_BLOCK;
 342 #ifndef PRODUCT
 343   SharedRuntime::_new_array_ctr++;            // new array requires GC
 344 #endif
 345   assert(check_compiled_frame(current), "incorrect caller");
 346 
 347   // Scavenge and allocate an instance.
 348   oop result;
 349 
 350   if (array_type->is_typeArray_klass()) {
 351     // The oopFactory likes to work with the element type.
 352     // (We could bypass the oopFactory, since it doesn't add much value.)
 353     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 354     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 355   } else {
 356     // Although the oopFactory likes to work with the elem_type,
 357     // the compiler prefers the array_type, since it must already have
 358     // that latter value in hand for the fast path.
 359     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 360     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 361     result = oopFactory::new_objArray(elem_type, len, THREAD);
 362   }
 363 
 364   // Pass oops back through thread local storage.  Our apparent type to Java
 365   // is that we return an oop, but we can block on exit from this routine and
 366   // a GC can trash the oop in C's return register.  The generated stub will
 367   // fetch the oop from TLS after any possible GC.
 368   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 369   current->set_vm_result_oop(result);
 370   JRT_BLOCK_END;
 371 
 372   // inform GC that we won't do card marks for initializing writes.
 373   SharedRuntime::on_slowpath_allocation_exit(current);
 374 JRT_END
 375 
 376 // array allocation without zeroing
 377 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 378   JRT_BLOCK;
 379 #ifndef PRODUCT
 380   SharedRuntime::_new_array_ctr++;            // new array requires GC
 381 #endif
 382   assert(check_compiled_frame(current), "incorrect caller");
 383 
 384   // Scavenge and allocate an instance.
 385   oop result;
 386 
 387   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 388   // The oopFactory likes to work with the element type.
 389   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 390   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 391 
 392   // Pass oops back through thread local storage.  Our apparent type to Java
 393   // is that we return an oop, but we can block on exit from this routine and
 394   // a GC can trash the oop in C's return register.  The generated stub will
 395   // fetch the oop from TLS after any possible GC.
 396   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 397   current->set_vm_result_oop(result);
 398   JRT_BLOCK_END;
 399 
 400 
 401   // inform GC that we won't do card marks for initializing writes.
 402   SharedRuntime::on_slowpath_allocation_exit(current);
 403 
 404   oop result = current->vm_result_oop();
 405   if ((len > 0) && (result != nullptr) &&
 406       is_deoptimized_caller_frame(current)) {
 407     // Zero array here if the caller is deoptimized.
 408     const size_t size = TypeArrayKlass::cast(array_type)->oop_size(result, result->mark());
 409     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 410     size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
 411     assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
 412     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 413     if (!is_aligned(hs_bytes, BytesPerLong)) {
 414       *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
 415       hs_bytes += BytesPerInt;
 416     }
 417 
 418     // Optimized zeroing.
 419     assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
 420     const size_t aligned_hs = hs_bytes / BytesPerLong;
 421     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 422   }
 423 
 424 JRT_END
 425 
 426 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 427 
 428 // multianewarray for 2 dimensions
 429 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 430 #ifndef PRODUCT
 431   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 432 #endif
 433   assert(check_compiled_frame(current), "incorrect caller");
 434   assert(elem_type->is_klass(), "not a class");
 435   jint dims[2];
 436   dims[0] = len1;
 437   dims[1] = len2;
 438   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 439   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 440   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 441   current->set_vm_result_oop(obj);
 442 JRT_END
 443 
 444 // multianewarray for 3 dimensions
 445 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 446 #ifndef PRODUCT
 447   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 448 #endif
 449   assert(check_compiled_frame(current), "incorrect caller");
 450   assert(elem_type->is_klass(), "not a class");
 451   jint dims[3];
 452   dims[0] = len1;
 453   dims[1] = len2;
 454   dims[2] = len3;
 455   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 456   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 457   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 458   current->set_vm_result_oop(obj);
 459 JRT_END
 460 
 461 // multianewarray for 4 dimensions
 462 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 463 #ifndef PRODUCT
 464   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 465 #endif
 466   assert(check_compiled_frame(current), "incorrect caller");
 467   assert(elem_type->is_klass(), "not a class");
 468   jint dims[4];
 469   dims[0] = len1;
 470   dims[1] = len2;
 471   dims[2] = len3;
 472   dims[3] = len4;
 473   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 474   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 475   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 476   current->set_vm_result_oop(obj);
 477 JRT_END
 478 
 479 // multianewarray for 5 dimensions
 480 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 481 #ifndef PRODUCT
 482   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 483 #endif
 484   assert(check_compiled_frame(current), "incorrect caller");
 485   assert(elem_type->is_klass(), "not a class");
 486   jint dims[5];
 487   dims[0] = len1;
 488   dims[1] = len2;
 489   dims[2] = len3;
 490   dims[3] = len4;
 491   dims[4] = len5;
 492   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 493   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 494   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 495   current->set_vm_result_oop(obj);
 496 JRT_END
 497 
 498 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 499   assert(check_compiled_frame(current), "incorrect caller");
 500   assert(elem_type->is_klass(), "not a class");
 501   assert(oop(dims)->is_typeArray(), "not an array");
 502 
 503   ResourceMark rm;
 504   jint len = dims->length();
 505   assert(len > 0, "Dimensions array should contain data");
 506   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 507   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 508                                        c_dims, len);
 509 
 510   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 511   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 512   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 513   current->set_vm_result_oop(obj);
 514 JRT_END
 515 
 516 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 517 
 518   // Very few notify/notifyAll operations find any threads on the waitset, so
 519   // the dominant fast-path is to simply return.
 520   // Relatedly, it's critical that notify/notifyAll be fast in order to
 521   // reduce lock hold times.
 522   if (!SafepointSynchronize::is_synchronizing()) {
 523     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 524       return;
 525     }
 526   }
 527 
 528   // This is the case the fast-path above isn't provisioned to handle.
 529   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 530   // (The fast-path is just a degenerate variant of the slow-path).
 531   // Perform the dreaded state transition and pass control into the slow-path.
 532   JRT_BLOCK;
 533   Handle h_obj(current, obj);
 534   ObjectSynchronizer::notify(h_obj, CHECK);
 535   JRT_BLOCK_END;
 536 JRT_END
 537 
 538 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 539 
 540   if (!SafepointSynchronize::is_synchronizing() ) {
 541     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 542       return;
 543     }
 544   }
 545 
 546   // This is the case the fast-path above isn't provisioned to handle.
 547   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 548   // (The fast-path is just a degenerate variant of the slow-path).
 549   // Perform the dreaded state transition and pass control into the slow-path.
 550   JRT_BLOCK;
 551   Handle h_obj(current, obj);
 552   ObjectSynchronizer::notifyall(h_obj, CHECK);
 553   JRT_BLOCK_END;
 554 JRT_END
 555 
 556 JRT_ENTRY(void, OptoRuntime::vthread_end_first_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
 557   MountUnmountDisabler::end_transition(current, vt, true /*is_mount*/, true /*is_thread_start*/);
 558 JRT_END
 559 
 560 JRT_ENTRY(void, OptoRuntime::vthread_start_final_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
 561   java_lang_Thread::set_is_in_vthread_transition(vt, false);
 562   current->set_is_in_vthread_transition(false);
 563   MountUnmountDisabler::start_transition(current, vt, false /*is_mount */, true /*is_thread_end*/);
 564 JRT_END
 565 
 566 JRT_ENTRY(void, OptoRuntime::vthread_start_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
 567   java_lang_Thread::set_is_in_vthread_transition(vt, false);
 568   current->set_is_in_vthread_transition(false);
 569   MountUnmountDisabler::start_transition(current, vt, is_mount, false /*is_thread_end*/);
 570 JRT_END
 571 
 572 JRT_ENTRY(void, OptoRuntime::vthread_end_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
 573   MountUnmountDisabler::end_transition(current, vt, is_mount, false /*is_thread_start*/);
 574 JRT_END
 575 
 576 static const TypeFunc* make_new_instance_Type() {
 577   // create input type (domain)
 578   const Type **fields = TypeTuple::fields(1);
 579   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 580   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 581 
 582   // create result type (range)
 583   fields = TypeTuple::fields(1);
 584   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 585 
 586   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 587 
 588   return TypeFunc::make(domain, range);
 589 }
 590 
 591 static const TypeFunc* make_vthread_transition_Type() {
 592   // create input type (domain)
 593   const Type **fields = TypeTuple::fields(2);
 594   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // VirtualThread oop
 595   fields[TypeFunc::Parms+1] = TypeInt::BOOL;        // jboolean
 596   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 597 
 598   // no result type needed
 599   fields = TypeTuple::fields(1);
 600   fields[TypeFunc::Parms+0] = nullptr; // void
 601   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 602 
 603   return TypeFunc::make(domain,range);
 604 }
 605 
 606 static const TypeFunc* make_athrow_Type() {
 607   // create input type (domain)
 608   const Type **fields = TypeTuple::fields(1);
 609   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 610   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 611 
 612   // create result type (range)
 613   fields = TypeTuple::fields(0);
 614 
 615   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 616 
 617   return TypeFunc::make(domain, range);
 618 }
 619 
 620 static const TypeFunc* make_new_array_Type() {
 621   // create input type (domain)
 622   const Type **fields = TypeTuple::fields(2);
 623   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 624   fields[TypeFunc::Parms+1] = TypeInt::INT;       // array size
 625   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 626 
 627   // create result type (range)
 628   fields = TypeTuple::fields(1);
 629   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 630 
 631   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 632 
 633   return TypeFunc::make(domain, range);
 634 }
 635 
 636 const TypeFunc* OptoRuntime::multianewarray_Type(int ndim) {
 637   // create input type (domain)
 638   const int nargs = ndim + 1;
 639   const Type **fields = TypeTuple::fields(nargs);
 640   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 641   for( int i = 1; i < nargs; i++ )
 642     fields[TypeFunc::Parms + i] = TypeInt::INT;       // array size
 643   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+nargs, fields);
 644 
 645   // create result type (range)
 646   fields = TypeTuple::fields(1);
 647   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 648   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 649 
 650   return TypeFunc::make(domain, range);
 651 }
 652 
 653 static const TypeFunc* make_multianewarrayN_Type() {
 654   // create input type (domain)
 655   const Type **fields = TypeTuple::fields(2);
 656   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 657   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;   // array of dim sizes
 658   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 659 
 660   // create result type (range)
 661   fields = TypeTuple::fields(1);
 662   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 663   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 664 
 665   return TypeFunc::make(domain, range);
 666 }
 667 
 668 static const TypeFunc* make_uncommon_trap_Type() {
 669   // create input type (domain)
 670   const Type **fields = TypeTuple::fields(1);
 671   fields[TypeFunc::Parms+0] = TypeInt::INT; // trap_reason (deopt reason and action)
 672   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 673 
 674   // create result type (range)
 675   fields = TypeTuple::fields(0);
 676   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 677 
 678   return TypeFunc::make(domain, range);
 679 }
 680 
 681 //-----------------------------------------------------------------------------
 682 // Monitor Handling
 683 
 684 static const TypeFunc* make_complete_monitor_enter_Type() {
 685   // create input type (domain)
 686   const Type **fields = TypeTuple::fields(2);
 687   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 688   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // Address of stack location for lock
 689   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 690 
 691   // create result type (range)
 692   fields = TypeTuple::fields(0);
 693 
 694   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
 695 
 696   return TypeFunc::make(domain,range);
 697 }
 698 
 699 //-----------------------------------------------------------------------------
 700 
 701 static const TypeFunc* make_complete_monitor_exit_Type() {
 702   // create input type (domain)
 703   const Type **fields = TypeTuple::fields(3);
 704   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 705   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;    // Address of stack location for lock - BasicLock
 706   fields[TypeFunc::Parms+2] = TypeRawPtr::BOTTOM;    // Thread pointer (Self)
 707   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+3, fields);
 708 
 709   // create result type (range)
 710   fields = TypeTuple::fields(0);
 711 
 712   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 713 
 714   return TypeFunc::make(domain, range);
 715 }
 716 
 717 static const TypeFunc* make_monitor_notify_Type() {
 718   // create input type (domain)
 719   const Type **fields = TypeTuple::fields(1);
 720   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 721   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 722 
 723   // create result type (range)
 724   fields = TypeTuple::fields(0);
 725   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 726   return TypeFunc::make(domain, range);
 727 }
 728 
 729 static const TypeFunc* make_flush_windows_Type() {
 730   // create input type (domain)
 731   const Type** fields = TypeTuple::fields(1);
 732   fields[TypeFunc::Parms+0] = nullptr; // void
 733   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
 734 
 735   // create result type
 736   fields = TypeTuple::fields(1);
 737   fields[TypeFunc::Parms+0] = nullptr; // void
 738   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 739 
 740   return TypeFunc::make(domain, range);
 741 }
 742 
 743 static const TypeFunc* make_l2f_Type() {
 744   // create input type (domain)
 745   const Type **fields = TypeTuple::fields(2);
 746   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 747   fields[TypeFunc::Parms+1] = Type::HALF;
 748   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 749 
 750   // create result type (range)
 751   fields = TypeTuple::fields(1);
 752   fields[TypeFunc::Parms+0] = Type::FLOAT;
 753   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 754 
 755   return TypeFunc::make(domain, range);
 756 }
 757 
 758 static const TypeFunc* make_modf_Type() {
 759   const Type **fields = TypeTuple::fields(2);
 760   fields[TypeFunc::Parms+0] = Type::FLOAT;
 761   fields[TypeFunc::Parms+1] = Type::FLOAT;
 762   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 763 
 764   // create result type (range)
 765   fields = TypeTuple::fields(1);
 766   fields[TypeFunc::Parms+0] = Type::FLOAT;
 767 
 768   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 769 
 770   return TypeFunc::make(domain, range);
 771 }
 772 
 773 static const TypeFunc* make_Math_D_D_Type() {
 774   // create input type (domain)
 775   const Type **fields = TypeTuple::fields(2);
 776   // Symbol* name of class to be loaded
 777   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 778   fields[TypeFunc::Parms+1] = Type::HALF;
 779   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 780 
 781   // create result type (range)
 782   fields = TypeTuple::fields(2);
 783   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 784   fields[TypeFunc::Parms+1] = Type::HALF;
 785   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 786 
 787   return TypeFunc::make(domain, range);
 788 }
 789 
 790 const TypeFunc* OptoRuntime::Math_Vector_Vector_Type(uint num_arg, const TypeVect* in_type, const TypeVect* out_type) {
 791   // create input type (domain)
 792   const Type **fields = TypeTuple::fields(num_arg);
 793   // Symbol* name of class to be loaded
 794   assert(num_arg > 0, "must have at least 1 input");
 795   for (uint i = 0; i < num_arg; i++) {
 796     fields[TypeFunc::Parms+i] = in_type;
 797   }
 798   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+num_arg, fields);
 799 
 800   // create result type (range)
 801   const uint num_ret = 1;
 802   fields = TypeTuple::fields(num_ret);
 803   fields[TypeFunc::Parms+0] = out_type;
 804   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+num_ret, fields);
 805 
 806   return TypeFunc::make(domain, range);
 807 }
 808 
 809 static const TypeFunc* make_Math_DD_D_Type() {
 810   const Type **fields = TypeTuple::fields(4);
 811   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 812   fields[TypeFunc::Parms+1] = Type::HALF;
 813   fields[TypeFunc::Parms+2] = Type::DOUBLE;
 814   fields[TypeFunc::Parms+3] = Type::HALF;
 815   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+4, fields);
 816 
 817   // create result type (range)
 818   fields = TypeTuple::fields(2);
 819   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 820   fields[TypeFunc::Parms+1] = Type::HALF;
 821   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 822 
 823   return TypeFunc::make(domain, range);
 824 }
 825 
 826 //-------------- currentTimeMillis, currentTimeNanos, etc
 827 
 828 static const TypeFunc* make_void_long_Type() {
 829   // create input type (domain)
 830   const Type **fields = TypeTuple::fields(0);
 831   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
 832 
 833   // create result type (range)
 834   fields = TypeTuple::fields(2);
 835   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 836   fields[TypeFunc::Parms+1] = Type::HALF;
 837   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 838 
 839   return TypeFunc::make(domain, range);
 840 }
 841 
 842 static const TypeFunc* make_void_void_Type() {
 843   // create input type (domain)
 844   const Type **fields = TypeTuple::fields(0);
 845   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
 846 
 847   // create result type (range)
 848   fields = TypeTuple::fields(0);
 849   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 850   return TypeFunc::make(domain, range);
 851 }
 852 
 853 static const TypeFunc* make_jfr_write_checkpoint_Type() {
 854   // create input type (domain)
 855   const Type **fields = TypeTuple::fields(0);
 856   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
 857 
 858   // create result type (range)
 859   fields = TypeTuple::fields(1);
 860   fields[TypeFunc::Parms] = TypeInstPtr::BOTTOM;
 861   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 1, fields);
 862   return TypeFunc::make(domain, range);
 863 }
 864 
 865 
 866 // Takes as parameters:
 867 // void *dest
 868 // long size
 869 // uchar byte
 870 
 871 static const TypeFunc* make_setmemory_Type() {
 872   // create input type (domain)
 873   int argcnt = NOT_LP64(3) LP64_ONLY(4);
 874   const Type** fields = TypeTuple::fields(argcnt);
 875   int argp = TypeFunc::Parms;
 876   fields[argp++] = TypePtr::NOTNULL;        // dest
 877   fields[argp++] = TypeX_X;                 // size
 878   LP64_ONLY(fields[argp++] = Type::HALF);   // size
 879   fields[argp++] = TypeInt::UBYTE;          // bytevalue
 880   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 881   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 882 
 883   // no result type needed
 884   fields = TypeTuple::fields(1);
 885   fields[TypeFunc::Parms+0] = nullptr; // void
 886   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 887   return TypeFunc::make(domain, range);
 888 }
 889 
 890 // arraycopy stub variations:
 891 enum ArrayCopyType {
 892   ac_fast,                      // void(ptr, ptr, size_t)
 893   ac_checkcast,                 //  int(ptr, ptr, size_t, size_t, ptr)
 894   ac_slow,                      // void(ptr, int, ptr, int, int)
 895   ac_generic                    //  int(ptr, int, ptr, int, int)
 896 };
 897 
 898 static const TypeFunc* make_arraycopy_Type(ArrayCopyType act) {
 899   // create input type (domain)
 900   int num_args      = (act == ac_fast ? 3 : 5);
 901   int num_size_args = (act == ac_fast ? 1 : act == ac_checkcast ? 2 : 0);
 902   int argcnt = num_args;
 903   LP64_ONLY(argcnt += num_size_args); // halfwords for lengths
 904   const Type** fields = TypeTuple::fields(argcnt);
 905   int argp = TypeFunc::Parms;
 906   fields[argp++] = TypePtr::NOTNULL;    // src
 907   if (num_size_args == 0) {
 908     fields[argp++] = TypeInt::INT;      // src_pos
 909   }
 910   fields[argp++] = TypePtr::NOTNULL;    // dest
 911   if (num_size_args == 0) {
 912     fields[argp++] = TypeInt::INT;      // dest_pos
 913     fields[argp++] = TypeInt::INT;      // length
 914   }
 915   while (num_size_args-- > 0) {
 916     fields[argp++] = TypeX_X;               // size in whatevers (size_t)
 917     LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
 918   }
 919   if (act == ac_checkcast) {
 920     fields[argp++] = TypePtr::NOTNULL;  // super_klass
 921   }
 922   assert(argp == TypeFunc::Parms+argcnt, "correct decoding of act");
 923   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 924 
 925   // create result type if needed
 926   int retcnt = (act == ac_checkcast || act == ac_generic ? 1 : 0);
 927   fields = TypeTuple::fields(1);
 928   if (retcnt == 0)
 929     fields[TypeFunc::Parms+0] = nullptr; // void
 930   else
 931     fields[TypeFunc::Parms+0] = TypeInt::INT; // status result, if needed
 932   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+retcnt, fields);
 933   return TypeFunc::make(domain, range);
 934 }
 935 
 936 static const TypeFunc* make_array_fill_Type() {
 937   const Type** fields;
 938   int argp = TypeFunc::Parms;
 939   // create input type (domain): pointer, int, size_t
 940   fields = TypeTuple::fields(3 LP64_ONLY( + 1));
 941   fields[argp++] = TypePtr::NOTNULL;
 942   fields[argp++] = TypeInt::INT;
 943   fields[argp++] = TypeX_X;               // size in whatevers (size_t)
 944   LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
 945   const TypeTuple *domain = TypeTuple::make(argp, fields);
 946 
 947   // create result type
 948   fields = TypeTuple::fields(1);
 949   fields[TypeFunc::Parms+0] = nullptr; // void
 950   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 951 
 952   return TypeFunc::make(domain, range);
 953 }
 954 
 955 static const TypeFunc* make_array_partition_Type() {
 956   // create input type (domain)
 957   int num_args = 7;
 958   int argcnt = num_args;
 959   const Type** fields = TypeTuple::fields(argcnt);
 960   int argp = TypeFunc::Parms;
 961   fields[argp++] = TypePtr::NOTNULL;  // array
 962   fields[argp++] = TypeInt::INT;      // element type
 963   fields[argp++] = TypeInt::INT;      // low
 964   fields[argp++] = TypeInt::INT;      // end
 965   fields[argp++] = TypePtr::NOTNULL;  // pivot_indices (int array)
 966   fields[argp++] = TypeInt::INT;      // indexPivot1
 967   fields[argp++] = TypeInt::INT;      // indexPivot2
 968   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 969   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 970 
 971   // no result type needed
 972   fields = TypeTuple::fields(1);
 973   fields[TypeFunc::Parms+0] = nullptr; // void
 974   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 975   return TypeFunc::make(domain, range);
 976 }
 977 
 978 static const TypeFunc* make_array_sort_Type() {
 979   // create input type (domain)
 980   int num_args      = 4;
 981   int argcnt = num_args;
 982   const Type** fields = TypeTuple::fields(argcnt);
 983   int argp = TypeFunc::Parms;
 984   fields[argp++] = TypePtr::NOTNULL;    // array
 985   fields[argp++] = TypeInt::INT;    // element type
 986   fields[argp++] = TypeInt::INT;    // fromIndex
 987   fields[argp++] = TypeInt::INT;    // toIndex
 988   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 989   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 990 
 991   // no result type needed
 992   fields = TypeTuple::fields(1);
 993   fields[TypeFunc::Parms+0] = nullptr; // void
 994   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 995   return TypeFunc::make(domain, range);
 996 }
 997 
 998 static const TypeFunc* make_aescrypt_block_Type() {
 999   // create input type (domain)
1000   int num_args      = 3;
1001   int argcnt = num_args;
1002   const Type** fields = TypeTuple::fields(argcnt);
1003   int argp = TypeFunc::Parms;
1004   fields[argp++] = TypePtr::NOTNULL;    // src
1005   fields[argp++] = TypePtr::NOTNULL;    // dest
1006   fields[argp++] = TypePtr::NOTNULL;    // k array
1007   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1008   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1009 
1010   // no result type needed
1011   fields = TypeTuple::fields(1);
1012   fields[TypeFunc::Parms+0] = nullptr; // void
1013   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1014   return TypeFunc::make(domain, range);
1015 }
1016 
1017 static const TypeFunc* make_updateBytesCRC32_Type() {
1018   // create input type (domain)
1019   int num_args      = 3;
1020   int argcnt = num_args;
1021   const Type** fields = TypeTuple::fields(argcnt);
1022   int argp = TypeFunc::Parms;
1023   fields[argp++] = TypeInt::INT;        // crc
1024   fields[argp++] = TypePtr::NOTNULL;    // src
1025   fields[argp++] = TypeInt::INT;        // len
1026   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1027   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1028 
1029   // result type needed
1030   fields = TypeTuple::fields(1);
1031   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
1032   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1033   return TypeFunc::make(domain, range);
1034 }
1035 
1036 static const TypeFunc* make_updateBytesCRC32C_Type() {
1037   // create input type (domain)
1038   int num_args      = 4;
1039   int argcnt = num_args;
1040   const Type** fields = TypeTuple::fields(argcnt);
1041   int argp = TypeFunc::Parms;
1042   fields[argp++] = TypeInt::INT;        // crc
1043   fields[argp++] = TypePtr::NOTNULL;    // buf
1044   fields[argp++] = TypeInt::INT;        // len
1045   fields[argp++] = TypePtr::NOTNULL;    // table
1046   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1047   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1048 
1049   // result type needed
1050   fields = TypeTuple::fields(1);
1051   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
1052   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1053   return TypeFunc::make(domain, range);
1054 }
1055 
1056 static const TypeFunc* make_updateBytesAdler32_Type() {
1057   // create input type (domain)
1058   int num_args      = 3;
1059   int argcnt = num_args;
1060   const Type** fields = TypeTuple::fields(argcnt);
1061   int argp = TypeFunc::Parms;
1062   fields[argp++] = TypeInt::INT;        // crc
1063   fields[argp++] = TypePtr::NOTNULL;    // src + offset
1064   fields[argp++] = TypeInt::INT;        // len
1065   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1066   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1067 
1068   // result type needed
1069   fields = TypeTuple::fields(1);
1070   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
1071   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1072   return TypeFunc::make(domain, range);
1073 }
1074 
1075 static const TypeFunc* make_cipherBlockChaining_aescrypt_Type() {
1076   // create input type (domain)
1077   int num_args      = 5;
1078   int argcnt = num_args;
1079   const Type** fields = TypeTuple::fields(argcnt);
1080   int argp = TypeFunc::Parms;
1081   fields[argp++] = TypePtr::NOTNULL;    // src
1082   fields[argp++] = TypePtr::NOTNULL;    // dest
1083   fields[argp++] = TypePtr::NOTNULL;    // k array
1084   fields[argp++] = TypePtr::NOTNULL;    // r array
1085   fields[argp++] = TypeInt::INT;        // src len
1086   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1087   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1088 
1089   // returning cipher len (int)
1090   fields = TypeTuple::fields(1);
1091   fields[TypeFunc::Parms+0] = TypeInt::INT;
1092   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1093   return TypeFunc::make(domain, range);
1094 }
1095 
1096 static const TypeFunc* make_electronicCodeBook_aescrypt_Type() {
1097   // create input type (domain)
1098   int num_args = 4;
1099   int argcnt = num_args;
1100   const Type** fields = TypeTuple::fields(argcnt);
1101   int argp = TypeFunc::Parms;
1102   fields[argp++] = TypePtr::NOTNULL;    // src
1103   fields[argp++] = TypePtr::NOTNULL;    // dest
1104   fields[argp++] = TypePtr::NOTNULL;    // k array
1105   fields[argp++] = TypeInt::INT;        // src len
1106   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1107   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1108 
1109   // returning cipher len (int)
1110   fields = TypeTuple::fields(1);
1111   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1112   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1113   return TypeFunc::make(domain, range);
1114 }
1115 
1116 static const TypeFunc* make_counterMode_aescrypt_Type() {
1117   // create input type (domain)
1118   int num_args = 7;
1119   int argcnt = num_args;
1120   const Type** fields = TypeTuple::fields(argcnt);
1121   int argp = TypeFunc::Parms;
1122   fields[argp++] = TypePtr::NOTNULL; // src
1123   fields[argp++] = TypePtr::NOTNULL; // dest
1124   fields[argp++] = TypePtr::NOTNULL; // k array
1125   fields[argp++] = TypePtr::NOTNULL; // counter array
1126   fields[argp++] = TypeInt::INT; // src len
1127   fields[argp++] = TypePtr::NOTNULL; // saved_encCounter
1128   fields[argp++] = TypePtr::NOTNULL; // saved used addr
1129   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1130   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1131   // returning cipher len (int)
1132   fields = TypeTuple::fields(1);
1133   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1134   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1135   return TypeFunc::make(domain, range);
1136 }
1137 
1138 static const TypeFunc* make_galoisCounterMode_aescrypt_Type() {
1139   // create input type (domain)
1140   int num_args = 8;
1141   int argcnt = num_args;
1142   const Type** fields = TypeTuple::fields(argcnt);
1143   int argp = TypeFunc::Parms;
1144   fields[argp++] = TypePtr::NOTNULL; // byte[] in + inOfs
1145   fields[argp++] = TypeInt::INT;     // int len
1146   fields[argp++] = TypePtr::NOTNULL; // byte[] ct + ctOfs
1147   fields[argp++] = TypePtr::NOTNULL; // byte[] out + outOfs
1148   fields[argp++] = TypePtr::NOTNULL; // byte[] key from AESCrypt obj
1149   fields[argp++] = TypePtr::NOTNULL; // long[] state from GHASH obj
1150   fields[argp++] = TypePtr::NOTNULL; // long[] subkeyHtbl from GHASH obj
1151   fields[argp++] = TypePtr::NOTNULL; // byte[] counter from GCTR obj
1152 
1153   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1154   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1155   // returning cipher len (int)
1156   fields = TypeTuple::fields(1);
1157   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1158   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1159   return TypeFunc::make(domain, range);
1160 }
1161 
1162 static const TypeFunc* make_digestBase_implCompress_Type(bool is_sha3) {
1163   // create input type (domain)
1164   int num_args = is_sha3 ? 3 : 2;
1165   int argcnt = num_args;
1166   const Type** fields = TypeTuple::fields(argcnt);
1167   int argp = TypeFunc::Parms;
1168   fields[argp++] = TypePtr::NOTNULL; // buf
1169   fields[argp++] = TypePtr::NOTNULL; // state
1170   if (is_sha3) fields[argp++] = TypeInt::INT; // block_size
1171   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1172   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1173 
1174   // no result type needed
1175   fields = TypeTuple::fields(1);
1176   fields[TypeFunc::Parms+0] = nullptr; // void
1177   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1178   return TypeFunc::make(domain, range);
1179 }
1180 
1181 /*
1182  * int implCompressMultiBlock(byte[] b, int ofs, int limit)
1183  */
1184 static const TypeFunc* make_digestBase_implCompressMB_Type(bool is_sha3) {
1185   // create input type (domain)
1186   int num_args = is_sha3 ? 5 : 4;
1187   int argcnt = num_args;
1188   const Type** fields = TypeTuple::fields(argcnt);
1189   int argp = TypeFunc::Parms;
1190   fields[argp++] = TypePtr::NOTNULL; // buf
1191   fields[argp++] = TypePtr::NOTNULL; // state
1192   if (is_sha3) fields[argp++] = TypeInt::INT; // block_size
1193   fields[argp++] = TypeInt::INT;     // ofs
1194   fields[argp++] = TypeInt::INT;     // limit
1195   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1196   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1197 
1198   // returning ofs (int)
1199   fields = TypeTuple::fields(1);
1200   fields[TypeFunc::Parms+0] = TypeInt::INT; // ofs
1201   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1202   return TypeFunc::make(domain, range);
1203 }
1204 
1205 // SHAKE128Parallel doubleKeccak function
1206 static const TypeFunc* make_double_keccak_Type() {
1207     int argcnt = 2;
1208 
1209     const Type** fields = TypeTuple::fields(argcnt);
1210     int argp = TypeFunc::Parms;
1211     fields[argp++] = TypePtr::NOTNULL;      // status0
1212     fields[argp++] = TypePtr::NOTNULL;      // status1
1213 
1214     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1215     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1216 
1217     // result type needed
1218     fields = TypeTuple::fields(1);
1219     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1220     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1221     return TypeFunc::make(domain, range);
1222 }
1223 
1224 static const TypeFunc* make_multiplyToLen_Type() {
1225   // create input type (domain)
1226   int num_args      = 5;
1227   int argcnt = num_args;
1228   const Type** fields = TypeTuple::fields(argcnt);
1229   int argp = TypeFunc::Parms;
1230   fields[argp++] = TypePtr::NOTNULL;    // x
1231   fields[argp++] = TypeInt::INT;        // xlen
1232   fields[argp++] = TypePtr::NOTNULL;    // y
1233   fields[argp++] = TypeInt::INT;        // ylen
1234   fields[argp++] = TypePtr::NOTNULL;    // z
1235   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1236   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1237 
1238   // no result type needed
1239   fields = TypeTuple::fields(1);
1240   fields[TypeFunc::Parms+0] = nullptr;
1241   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1242   return TypeFunc::make(domain, range);
1243 }
1244 
1245 static const TypeFunc* make_squareToLen_Type() {
1246   // create input type (domain)
1247   int num_args      = 4;
1248   int argcnt = num_args;
1249   const Type** fields = TypeTuple::fields(argcnt);
1250   int argp = TypeFunc::Parms;
1251   fields[argp++] = TypePtr::NOTNULL;    // x
1252   fields[argp++] = TypeInt::INT;        // len
1253   fields[argp++] = TypePtr::NOTNULL;    // z
1254   fields[argp++] = TypeInt::INT;        // zlen
1255   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1256   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1257 
1258   // no result type needed
1259   fields = TypeTuple::fields(1);
1260   fields[TypeFunc::Parms+0] = nullptr;
1261   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1262   return TypeFunc::make(domain, range);
1263 }
1264 
1265 static const TypeFunc* make_mulAdd_Type() {
1266   // create input type (domain)
1267   int num_args      = 5;
1268   int argcnt = num_args;
1269   const Type** fields = TypeTuple::fields(argcnt);
1270   int argp = TypeFunc::Parms;
1271   fields[argp++] = TypePtr::NOTNULL;    // out
1272   fields[argp++] = TypePtr::NOTNULL;    // in
1273   fields[argp++] = TypeInt::INT;        // offset
1274   fields[argp++] = TypeInt::INT;        // len
1275   fields[argp++] = TypeInt::INT;        // k
1276   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1277   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1278 
1279   // returning carry (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 static const TypeFunc* make_montgomeryMultiply_Type() {
1287   // create input type (domain)
1288   int num_args      = 7;
1289   int argcnt = num_args;
1290   const Type** fields = TypeTuple::fields(argcnt);
1291   int argp = TypeFunc::Parms;
1292   fields[argp++] = TypePtr::NOTNULL;    // a
1293   fields[argp++] = TypePtr::NOTNULL;    // b
1294   fields[argp++] = TypePtr::NOTNULL;    // n
1295   fields[argp++] = TypeInt::INT;        // len
1296   fields[argp++] = TypeLong::LONG;      // inv
1297   fields[argp++] = Type::HALF;
1298   fields[argp++] = TypePtr::NOTNULL;    // result
1299   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1300   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1301 
1302   // result type needed
1303   fields = TypeTuple::fields(1);
1304   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1305 
1306   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1307   return TypeFunc::make(domain, range);
1308 }
1309 
1310 static const TypeFunc* make_montgomerySquare_Type() {
1311   // create input type (domain)
1312   int num_args      = 6;
1313   int argcnt = num_args;
1314   const Type** fields = TypeTuple::fields(argcnt);
1315   int argp = TypeFunc::Parms;
1316   fields[argp++] = TypePtr::NOTNULL;    // a
1317   fields[argp++] = TypePtr::NOTNULL;    // n
1318   fields[argp++] = TypeInt::INT;        // len
1319   fields[argp++] = TypeLong::LONG;      // inv
1320   fields[argp++] = Type::HALF;
1321   fields[argp++] = TypePtr::NOTNULL;    // result
1322   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1323   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1324 
1325   // result type needed
1326   fields = TypeTuple::fields(1);
1327   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1328 
1329   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1330   return TypeFunc::make(domain, range);
1331 }
1332 
1333 static const TypeFunc* make_bigIntegerShift_Type() {
1334   int argcnt = 5;
1335   const Type** fields = TypeTuple::fields(argcnt);
1336   int argp = TypeFunc::Parms;
1337   fields[argp++] = TypePtr::NOTNULL;    // newArr
1338   fields[argp++] = TypePtr::NOTNULL;    // oldArr
1339   fields[argp++] = TypeInt::INT;        // newIdx
1340   fields[argp++] = TypeInt::INT;        // shiftCount
1341   fields[argp++] = TypeInt::INT;        // numIter
1342   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1343   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1344 
1345   // no result type needed
1346   fields = TypeTuple::fields(1);
1347   fields[TypeFunc::Parms + 0] = nullptr;
1348   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1349   return TypeFunc::make(domain, range);
1350 }
1351 
1352 static const TypeFunc* make_vectorizedMismatch_Type() {
1353   // create input type (domain)
1354   int num_args = 4;
1355   int argcnt = num_args;
1356   const Type** fields = TypeTuple::fields(argcnt);
1357   int argp = TypeFunc::Parms;
1358   fields[argp++] = TypePtr::NOTNULL;    // obja
1359   fields[argp++] = TypePtr::NOTNULL;    // objb
1360   fields[argp++] = TypeInt::INT;        // length, number of elements
1361   fields[argp++] = TypeInt::INT;        // log2scale, element size
1362   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1363   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1364 
1365   //return mismatch index (int)
1366   fields = TypeTuple::fields(1);
1367   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1368   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1369   return TypeFunc::make(domain, range);
1370 }
1371 
1372 static const TypeFunc* make_ghash_processBlocks_Type() {
1373   int argcnt = 4;
1374 
1375   const Type** fields = TypeTuple::fields(argcnt);
1376   int argp = TypeFunc::Parms;
1377   fields[argp++] = TypePtr::NOTNULL;    // state
1378   fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1379   fields[argp++] = TypePtr::NOTNULL;    // data
1380   fields[argp++] = TypeInt::INT;        // blocks
1381   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1382   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1383 
1384   // result type needed
1385   fields = TypeTuple::fields(1);
1386   fields[TypeFunc::Parms+0] = nullptr; // void
1387   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1388   return TypeFunc::make(domain, range);
1389 }
1390 
1391 static const TypeFunc* make_chacha20Block_Type() {
1392   int argcnt = 2;
1393 
1394   const Type** fields = TypeTuple::fields(argcnt);
1395   int argp = TypeFunc::Parms;
1396   fields[argp++] = TypePtr::NOTNULL;      // state
1397   fields[argp++] = TypePtr::NOTNULL;      // result
1398 
1399   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1400   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1401 
1402   // result type needed
1403   fields = TypeTuple::fields(1);
1404   fields[TypeFunc::Parms + 0] = TypeInt::INT;     // key stream outlen as int
1405   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1406   return TypeFunc::make(domain, range);
1407 }
1408 
1409 // Kyber NTT function
1410 static const TypeFunc* make_kyberNtt_Type() {
1411     int argcnt = 2;
1412 
1413     const Type** fields = TypeTuple::fields(argcnt);
1414     int argp = TypeFunc::Parms;
1415     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1416     fields[argp++] = TypePtr::NOTNULL;      // NTT zetas
1417 
1418     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1419     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1420 
1421     // result type needed
1422     fields = TypeTuple::fields(1);
1423     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1424     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1425     return TypeFunc::make(domain, range);
1426 }
1427 
1428 // Kyber inverse NTT function
1429 static const TypeFunc* make_kyberInverseNtt_Type() {
1430     int argcnt = 2;
1431 
1432     const Type** fields = TypeTuple::fields(argcnt);
1433     int argp = TypeFunc::Parms;
1434     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1435     fields[argp++] = TypePtr::NOTNULL;      // inverse NTT zetas
1436 
1437     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1438     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1439 
1440     // result type needed
1441     fields = TypeTuple::fields(1);
1442     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1443     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1444     return TypeFunc::make(domain, range);
1445 }
1446 
1447 // Kyber NTT multiply function
1448 static const TypeFunc* make_kyberNttMult_Type() {
1449     int argcnt = 4;
1450 
1451     const Type** fields = TypeTuple::fields(argcnt);
1452     int argp = TypeFunc::Parms;
1453     fields[argp++] = TypePtr::NOTNULL;      // result
1454     fields[argp++] = TypePtr::NOTNULL;      // ntta
1455     fields[argp++] = TypePtr::NOTNULL;      // nttb
1456     fields[argp++] = TypePtr::NOTNULL;      // NTT multiply zetas
1457 
1458     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1459     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1460 
1461     // result type needed
1462     fields = TypeTuple::fields(1);
1463     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1464     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1465     return TypeFunc::make(domain, range);
1466 }
1467 
1468 // Kyber add 2 polynomials function
1469 static const TypeFunc* make_kyberAddPoly_2_Type() {
1470     int argcnt = 3;
1471 
1472     const Type** fields = TypeTuple::fields(argcnt);
1473     int argp = TypeFunc::Parms;
1474     fields[argp++] = TypePtr::NOTNULL;      // result
1475     fields[argp++] = TypePtr::NOTNULL;      // a
1476     fields[argp++] = TypePtr::NOTNULL;      // b
1477 
1478     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1479     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1480 
1481     // result type needed
1482     fields = TypeTuple::fields(1);
1483     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1484     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1485     return TypeFunc::make(domain, range);
1486 }
1487 
1488 
1489 // Kyber add 3 polynomials function
1490 static const TypeFunc* make_kyberAddPoly_3_Type() {
1491     int argcnt = 4;
1492 
1493     const Type** fields = TypeTuple::fields(argcnt);
1494     int argp = TypeFunc::Parms;
1495     fields[argp++] = TypePtr::NOTNULL;      // result
1496     fields[argp++] = TypePtr::NOTNULL;      // a
1497     fields[argp++] = TypePtr::NOTNULL;      // b
1498     fields[argp++] = TypePtr::NOTNULL;      // c
1499 
1500     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1501     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1502 
1503     // result type needed
1504     fields = TypeTuple::fields(1);
1505     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1506     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1507     return TypeFunc::make(domain, range);
1508 }
1509 
1510 
1511 // Kyber XOF output parsing into polynomial coefficients candidates
1512 // or decompress(12,...) function
1513 static const TypeFunc* make_kyber12To16_Type() {
1514     int argcnt = 4;
1515 
1516     const Type** fields = TypeTuple::fields(argcnt);
1517     int argp = TypeFunc::Parms;
1518     fields[argp++] = TypePtr::NOTNULL;      // condensed
1519     fields[argp++] = TypeInt::INT;          // condensedOffs
1520     fields[argp++] = TypePtr::NOTNULL;      // parsed
1521     fields[argp++] = TypeInt::INT;          // parsedLength
1522 
1523     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1524     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1525 
1526     // result type needed
1527     fields = TypeTuple::fields(1);
1528     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1529     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1530     return TypeFunc::make(domain, range);
1531 }
1532 
1533 // Kyber Barrett reduce function
1534 static const TypeFunc* make_kyberBarrettReduce_Type() {
1535     int argcnt = 1;
1536 
1537     const Type** fields = TypeTuple::fields(argcnt);
1538     int argp = TypeFunc::Parms;
1539     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1540 
1541     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1542     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1543 
1544     // result type needed
1545     fields = TypeTuple::fields(1);
1546     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1547     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1548     return TypeFunc::make(domain, range);
1549 }
1550 
1551 // Dilithium NTT function except for the final "normalization" to |coeff| < Q
1552 static const TypeFunc* make_dilithiumAlmostNtt_Type() {
1553     int argcnt = 2;
1554 
1555     const Type** fields = TypeTuple::fields(argcnt);
1556     int argp = TypeFunc::Parms;
1557     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1558     fields[argp++] = TypePtr::NOTNULL;      // NTT zetas
1559 
1560     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1561     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1562 
1563     // result type needed
1564     fields = TypeTuple::fields(1);
1565     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1566     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1567     return TypeFunc::make(domain, range);
1568 }
1569 
1570 // Dilithium inverse NTT function except the final mod Q division by 2^256
1571 static const TypeFunc* make_dilithiumAlmostInverseNtt_Type() {
1572     int argcnt = 2;
1573 
1574     const Type** fields = TypeTuple::fields(argcnt);
1575     int argp = TypeFunc::Parms;
1576     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1577     fields[argp++] = TypePtr::NOTNULL;      // inverse NTT zetas
1578 
1579     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1580     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1581 
1582     // result type needed
1583     fields = TypeTuple::fields(1);
1584     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1585     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1586     return TypeFunc::make(domain, range);
1587 }
1588 
1589 // Dilithium NTT multiply function
1590 static const TypeFunc* make_dilithiumNttMult_Type() {
1591     int argcnt = 3;
1592 
1593     const Type** fields = TypeTuple::fields(argcnt);
1594     int argp = TypeFunc::Parms;
1595     fields[argp++] = TypePtr::NOTNULL;      // result
1596     fields[argp++] = TypePtr::NOTNULL;      // ntta
1597     fields[argp++] = TypePtr::NOTNULL;      // nttb
1598 
1599     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1600     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1601 
1602     // result type needed
1603     fields = TypeTuple::fields(1);
1604     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1605     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1606     return TypeFunc::make(domain, range);
1607 }
1608 
1609 // Dilithium Montgomery multiply a polynome coefficient array by a constant
1610 static const TypeFunc* make_dilithiumMontMulByConstant_Type() {
1611     int argcnt = 2;
1612 
1613     const Type** fields = TypeTuple::fields(argcnt);
1614     int argp = TypeFunc::Parms;
1615     fields[argp++] = TypePtr::NOTNULL;      // coeffs
1616     fields[argp++] = TypeInt::INT;          // constant multiplier
1617 
1618     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1619     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1620 
1621     // result type needed
1622     fields = TypeTuple::fields(1);
1623     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1624     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1625     return TypeFunc::make(domain, range);
1626 }
1627 
1628 // Dilithium decompose polynomial
1629 static const TypeFunc* make_dilithiumDecomposePoly_Type() {
1630     int argcnt = 5;
1631 
1632     const Type** fields = TypeTuple::fields(argcnt);
1633     int argp = TypeFunc::Parms;
1634     fields[argp++] = TypePtr::NOTNULL;      // input
1635     fields[argp++] = TypePtr::NOTNULL;      // lowPart
1636     fields[argp++] = TypePtr::NOTNULL;      // highPart
1637     fields[argp++] = TypeInt::INT;          // 2 * gamma2
1638     fields[argp++] = TypeInt::INT;          // multiplier
1639 
1640     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1641     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1642 
1643     // result type needed
1644     fields = TypeTuple::fields(1);
1645     fields[TypeFunc::Parms + 0] = TypeInt::INT;
1646     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1647     return TypeFunc::make(domain, range);
1648 }
1649 
1650 static const TypeFunc* make_base64_encodeBlock_Type() {
1651   int argcnt = 6;
1652 
1653   const Type** fields = TypeTuple::fields(argcnt);
1654   int argp = TypeFunc::Parms;
1655   fields[argp++] = TypePtr::NOTNULL;    // src array
1656   fields[argp++] = TypeInt::INT;        // offset
1657   fields[argp++] = TypeInt::INT;        // length
1658   fields[argp++] = TypePtr::NOTNULL;    // dest array
1659   fields[argp++] = TypeInt::INT;       // dp
1660   fields[argp++] = TypeInt::BOOL;       // isURL
1661   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1662   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1663 
1664   // result type needed
1665   fields = TypeTuple::fields(1);
1666   fields[TypeFunc::Parms + 0] = nullptr; // void
1667   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1668   return TypeFunc::make(domain, range);
1669 }
1670 
1671 static const TypeFunc* make_string_IndexOf_Type() {
1672   int argcnt = 4;
1673 
1674   const Type** fields = TypeTuple::fields(argcnt);
1675   int argp = TypeFunc::Parms;
1676   fields[argp++] = TypePtr::NOTNULL;    // haystack array
1677   fields[argp++] = TypeInt::INT;        // haystack length
1678   fields[argp++] = TypePtr::NOTNULL;    // needle array
1679   fields[argp++] = TypeInt::INT;        // needle length
1680   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1681   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1682 
1683   // result type needed
1684   fields = TypeTuple::fields(1);
1685   fields[TypeFunc::Parms + 0] = TypeInt::INT; // Index of needle in haystack
1686   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1687   return TypeFunc::make(domain, range);
1688 }
1689 
1690 static const TypeFunc* make_base64_decodeBlock_Type() {
1691   int argcnt = 7;
1692 
1693   const Type** fields = TypeTuple::fields(argcnt);
1694   int argp = TypeFunc::Parms;
1695   fields[argp++] = TypePtr::NOTNULL;    // src array
1696   fields[argp++] = TypeInt::INT;        // src offset
1697   fields[argp++] = TypeInt::INT;        // src length
1698   fields[argp++] = TypePtr::NOTNULL;    // dest array
1699   fields[argp++] = TypeInt::INT;        // dest offset
1700   fields[argp++] = TypeInt::BOOL;       // isURL
1701   fields[argp++] = TypeInt::BOOL;       // isMIME
1702   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1703   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1704 
1705   // result type needed
1706   fields = TypeTuple::fields(1);
1707   fields[TypeFunc::Parms + 0] = TypeInt::INT; // count of bytes written to dst
1708   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1709   return TypeFunc::make(domain, range);
1710 }
1711 
1712 static const TypeFunc* make_poly1305_processBlocks_Type() {
1713   int argcnt = 4;
1714 
1715   const Type** fields = TypeTuple::fields(argcnt);
1716   int argp = TypeFunc::Parms;
1717   fields[argp++] = TypePtr::NOTNULL;    // input array
1718   fields[argp++] = TypeInt::INT;        // input length
1719   fields[argp++] = TypePtr::NOTNULL;    // accumulator array
1720   fields[argp++] = TypePtr::NOTNULL;    // r array
1721   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1722   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1723 
1724   // result type needed
1725   fields = TypeTuple::fields(1);
1726   fields[TypeFunc::Parms + 0] = nullptr; // void
1727   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1728   return TypeFunc::make(domain, range);
1729 }
1730 
1731 static const TypeFunc* make_intpoly_montgomeryMult_P256_Type() {
1732   int argcnt = 3;
1733 
1734   const Type** fields = TypeTuple::fields(argcnt);
1735   int argp = TypeFunc::Parms;
1736   fields[argp++] = TypePtr::NOTNULL;    // a array
1737   fields[argp++] = TypePtr::NOTNULL;    // b array
1738   fields[argp++] = TypePtr::NOTNULL;    // r(esult) array
1739   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1740   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1741 
1742   // result type needed
1743   fields = TypeTuple::fields(1);
1744   fields[TypeFunc::Parms + 0] = nullptr; // void
1745   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1746   return TypeFunc::make(domain, range);
1747 }
1748 
1749 static const TypeFunc* make_intpoly_assign_Type() {
1750   int argcnt = 4;
1751 
1752   const Type** fields = TypeTuple::fields(argcnt);
1753   int argp = TypeFunc::Parms;
1754   fields[argp++] = TypeInt::INT;        // set flag
1755   fields[argp++] = TypePtr::NOTNULL;    // a array (result)
1756   fields[argp++] = TypePtr::NOTNULL;    // b array (if set is set)
1757   fields[argp++] = TypeInt::INT;        // array length
1758   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1759   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1760 
1761   // result type needed
1762   fields = TypeTuple::fields(1);
1763   fields[TypeFunc::Parms + 0] = nullptr; // void
1764   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1765   return TypeFunc::make(domain, range);
1766 }
1767 
1768 //------------- Interpreter state for on stack replacement
1769 static const TypeFunc* make_osr_end_Type() {
1770   // create input type (domain)
1771   const Type **fields = TypeTuple::fields(1);
1772   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1773   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1774 
1775   // create result type
1776   fields = TypeTuple::fields(1);
1777   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1778   fields[TypeFunc::Parms+0] = nullptr; // void
1779   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1780   return TypeFunc::make(domain, range);
1781 }
1782 
1783 #ifndef PRODUCT
1784 static void debug_print_convert_type(const Type** fields, int* argp, Node *parm) {
1785   const BasicType bt = parm->bottom_type()->basic_type();
1786   fields[(*argp)++] = Type::get_const_basic_type(bt);
1787   if (bt == T_LONG || bt == T_DOUBLE) {
1788     fields[(*argp)++] = Type::HALF;
1789   }
1790 }
1791 
1792 static void update_arg_cnt(const Node* parm, int* arg_cnt) {
1793   (*arg_cnt)++;
1794   const BasicType bt = parm->bottom_type()->basic_type();
1795   if (bt == T_LONG || bt == T_DOUBLE) {
1796     (*arg_cnt)++;
1797   }
1798 }
1799 
1800 const TypeFunc* OptoRuntime::debug_print_Type(Node* parm0, Node* parm1,
1801                                         Node* parm2, Node* parm3,
1802                                         Node* parm4, Node* parm5,
1803                                         Node* parm6) {
1804   int argcnt = 1;
1805   if (parm0 != nullptr) { update_arg_cnt(parm0, &argcnt);
1806   if (parm1 != nullptr) { update_arg_cnt(parm1, &argcnt);
1807   if (parm2 != nullptr) { update_arg_cnt(parm2, &argcnt);
1808   if (parm3 != nullptr) { update_arg_cnt(parm3, &argcnt);
1809   if (parm4 != nullptr) { update_arg_cnt(parm4, &argcnt);
1810   if (parm5 != nullptr) { update_arg_cnt(parm5, &argcnt);
1811   if (parm6 != nullptr) { update_arg_cnt(parm6, &argcnt);
1812   /* close each nested if ===> */  } } } } } } }
1813 
1814   // create input type (domain)
1815   const Type** fields = TypeTuple::fields(argcnt);
1816   int argp = TypeFunc::Parms;
1817   fields[argp++] = TypePtr::NOTNULL;    // static string pointer
1818 
1819   if (parm0 != nullptr) { debug_print_convert_type(fields, &argp, parm0);
1820   if (parm1 != nullptr) { debug_print_convert_type(fields, &argp, parm1);
1821   if (parm2 != nullptr) { debug_print_convert_type(fields, &argp, parm2);
1822   if (parm3 != nullptr) { debug_print_convert_type(fields, &argp, parm3);
1823   if (parm4 != nullptr) { debug_print_convert_type(fields, &argp, parm4);
1824   if (parm5 != nullptr) { debug_print_convert_type(fields, &argp, parm5);
1825   if (parm6 != nullptr) { debug_print_convert_type(fields, &argp, parm6);
1826   /* close each nested if ===> */  } } } } } } }
1827 
1828   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1829   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1830 
1831   // no result type needed
1832   fields = TypeTuple::fields(1);
1833   fields[TypeFunc::Parms+0] = nullptr; // void
1834   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1835   return TypeFunc::make(domain, range);
1836 }
1837 #endif // PRODUCT
1838 
1839 //-------------------------------------------------------------------------------------
1840 // register policy
1841 
1842 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1843   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1844   switch (register_save_policy[reg]) {
1845     case 'C': return false; //SOC
1846     case 'E': return true ; //SOE
1847     case 'N': return false; //NS
1848     case 'A': return false; //AS
1849   }
1850   ShouldNotReachHere();
1851   return false;
1852 }
1853 
1854 //-----------------------------------------------------------------------
1855 // Exceptions
1856 //
1857 
1858 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1859 
1860 // The method is an entry that is always called by a C++ method not
1861 // directly from compiled code. Compiled code will call the C++ method following.
1862 // We can't allow async exception to be installed during  exception processing.
1863 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1864   // The frame we rethrow the exception to might not have been processed by the GC yet.
1865   // The stack watermark barrier takes care of detecting that and ensuring the frame
1866   // has updated oops.
1867   StackWatermarkSet::after_unwind(current);
1868 
1869   MACOS_AARCH64_ONLY(os::thread_wx_enable_write());
1870 
1871   // Do not confuse exception_oop with pending_exception. The exception_oop
1872   // is only used to pass arguments into the method. Not for general
1873   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1874   // the runtime stubs checks this on exit.
1875   assert(current->exception_oop() != nullptr, "exception oop is found");
1876   address handler_address = nullptr;
1877 
1878   Handle exception(current, current->exception_oop());
1879   address pc = current->exception_pc();
1880 
1881   // Clear out the exception oop and pc since looking up an
1882   // exception handler can cause class loading, which might throw an
1883   // exception and those fields are expected to be clear during
1884   // normal bytecode execution.
1885   current->clear_exception_oop_and_pc();
1886 
1887   LogTarget(Info, exceptions) lt;
1888   if (lt.is_enabled()) {
1889     LogStream ls(lt);
1890     trace_exception(&ls, exception(), pc, "");
1891   }
1892 
1893   // for AbortVMOnException flag
1894   Exceptions::debug_check_abort(exception);
1895 
1896 #ifdef ASSERT
1897   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1898     // should throw an exception here
1899     ShouldNotReachHere();
1900   }
1901 #endif
1902 
1903   // new exception handling: this method is entered only from adapters
1904   // exceptions from compiled java methods are handled in compiled code
1905   // using rethrow node
1906 
1907   nm = CodeCache::find_nmethod(pc);
1908   assert(nm != nullptr, "No NMethod found");
1909   if (nm->is_native_method()) {
1910     fatal("Native method should not have path to exception handling");
1911   } else {
1912     // we are switching to old paradigm: search for exception handler in caller_frame
1913     // instead in exception handler of caller_frame.sender()
1914 
1915     if (JvmtiExport::can_post_on_exceptions()) {
1916       // "Full-speed catching" is not necessary here,
1917       // since we're notifying the VM on every catch.
1918       // Force deoptimization and the rest of the lookup
1919       // will be fine.
1920       deoptimize_caller_frame(current);
1921     }
1922 
1923     // Check the stack guard pages.  If enabled, look for handler in this frame;
1924     // otherwise, forcibly unwind the frame.
1925     //
1926     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1927     bool force_unwind = !current->stack_overflow_state()->reguard_stack();
1928     bool deopting = false;
1929     if (nm->is_deopt_pc(pc)) {
1930       deopting = true;
1931       RegisterMap map(current,
1932                       RegisterMap::UpdateMap::skip,
1933                       RegisterMap::ProcessFrames::include,
1934                       RegisterMap::WalkContinuation::skip);
1935       frame deoptee = current->last_frame().sender(&map);
1936       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1937       // Adjust the pc back to the original throwing pc
1938       pc = deoptee.pc();
1939     }
1940 
1941     // If we are forcing an unwind because of stack overflow then deopt is
1942     // irrelevant since we are throwing the frame away anyway.
1943 
1944     if (deopting && !force_unwind) {
1945       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1946     } else {
1947 
1948       handler_address =
1949         force_unwind ? nullptr : nm->handler_for_exception_and_pc(exception, pc);
1950 
1951       if (handler_address == nullptr) {
1952         bool recursive_exception = false;
1953         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1954         assert (handler_address != nullptr, "must have compiled handler");
1955         // Update the exception cache only when the unwind was not forced
1956         // and there didn't happen another exception during the computation of the
1957         // compiled exception handler. Checking for exception oop equality is not
1958         // sufficient because some exceptions are pre-allocated and reused.
1959         if (!force_unwind && !recursive_exception) {
1960           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1961         }
1962       } else {
1963 #ifdef ASSERT
1964         bool recursive_exception = false;
1965         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1966         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1967                  p2i(handler_address), p2i(computed_address));
1968 #endif
1969       }
1970     }
1971 
1972     current->set_exception_pc(pc);
1973     current->set_exception_handler_pc(handler_address);
1974   }
1975 
1976   // Restore correct return pc.  Was saved above.
1977   current->set_exception_oop(exception());
1978   return handler_address;
1979 
1980 JRT_END
1981 
1982 // We are entering here from exception_blob
1983 // If there is a compiled exception handler in this method, we will continue there;
1984 // otherwise we will unwind the stack and continue at the caller of top frame method
1985 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1986 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1987 // we looked up the handler for has been deoptimized in the meantime. If it has been
1988 // we must not use the handler and instead return the deopt blob.
1989 address OptoRuntime::handle_exception_C(JavaThread* current) {
1990 //
1991 // We are in Java not VM and in debug mode we have a NoHandleMark
1992 //
1993 #ifndef PRODUCT
1994   SharedRuntime::_find_handler_ctr++;          // find exception handler
1995 #endif
1996   DEBUG_ONLY(NoHandleMark __hm;)
1997   nmethod* nm = nullptr;
1998   address handler_address = nullptr;
1999   {
2000     // Enter the VM
2001 
2002     ResetNoHandleMark rnhm;
2003     handler_address = handle_exception_C_helper(current, nm);
2004   }
2005 
2006   // Back in java: Use no oops, DON'T safepoint
2007 
2008   // Now check to see if the handler we are returning is in a now
2009   // deoptimized frame
2010 
2011   if (nm != nullptr) {
2012     RegisterMap map(current,
2013                     RegisterMap::UpdateMap::skip,
2014                     RegisterMap::ProcessFrames::skip,
2015                     RegisterMap::WalkContinuation::skip);
2016     frame caller = current->last_frame().sender(&map);
2017 #ifdef ASSERT
2018     assert(caller.is_compiled_frame(), "must be");
2019 #endif // ASSERT
2020     if (caller.is_deoptimized_frame()) {
2021       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
2022     }
2023   }
2024   return handler_address;
2025 }
2026 
2027 //------------------------------rethrow----------------------------------------
2028 // We get here after compiled code has executed a 'RethrowNode'.  The callee
2029 // is either throwing or rethrowing an exception.  The callee-save registers
2030 // have been restored, synchronized objects have been unlocked and the callee
2031 // stack frame has been removed.  The return address was passed in.
2032 // Exception oop is passed as the 1st argument.  This routine is then called
2033 // from the stub.  On exit, we know where to jump in the caller's code.
2034 // After this C code exits, the stub will pop his frame and end in a jump
2035 // (instead of a return).  We enter the caller's default handler.
2036 //
2037 // This must be JRT_LEAF:
2038 //     - caller will not change its state as we cannot block on exit,
2039 //       therefore raw_exception_handler_for_return_address is all it takes
2040 //       to handle deoptimized blobs
2041 //
2042 // However, there needs to be a safepoint check in the middle!  So compiled
2043 // safepoints are completely watertight.
2044 //
2045 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
2046 //
2047 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
2048 //
2049 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
2050   // ret_pc will have been loaded from the stack, so for AArch64 will be signed.
2051   AARCH64_PORT_ONLY(ret_pc = pauth_strip_verifiable(ret_pc));
2052 
2053 #ifndef PRODUCT
2054   SharedRuntime::_rethrow_ctr++;               // count rethrows
2055 #endif
2056   assert (exception != nullptr, "should have thrown a NullPointerException");
2057 #ifdef ASSERT
2058   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
2059     // should throw an exception here
2060     ShouldNotReachHere();
2061   }
2062 #endif
2063 
2064   thread->set_vm_result_oop(exception);
2065   // Frame not compiled (handles deoptimization blob)
2066   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
2067 }
2068 
2069 static const TypeFunc* make_rethrow_Type() {
2070   // create input type (domain)
2071   const Type **fields = TypeTuple::fields(1);
2072   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
2073   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2074 
2075   // create result type (range)
2076   fields = TypeTuple::fields(1);
2077   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
2078   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
2079 
2080   return TypeFunc::make(domain, range);
2081 }
2082 
2083 
2084 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
2085   // Deoptimize the caller before continuing, as the compiled
2086   // exception handler table may not be valid.
2087   if (DeoptimizeOnAllocationException && doit) {
2088     deoptimize_caller_frame(thread);
2089   }
2090 }
2091 
2092 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
2093   // Called from within the owner thread, so no need for safepoint
2094   RegisterMap reg_map(thread,
2095                       RegisterMap::UpdateMap::include,
2096                       RegisterMap::ProcessFrames::include,
2097                       RegisterMap::WalkContinuation::skip);
2098   frame stub_frame = thread->last_frame();
2099   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
2100   frame caller_frame = stub_frame.sender(&reg_map);
2101 
2102   // Deoptimize the caller frame.
2103   Deoptimization::deoptimize_frame(thread, caller_frame.id());
2104 }
2105 
2106 
2107 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
2108   // Called from within the owner thread, so no need for safepoint
2109   RegisterMap reg_map(thread,
2110                       RegisterMap::UpdateMap::include,
2111                       RegisterMap::ProcessFrames::include,
2112                       RegisterMap::WalkContinuation::skip);
2113   frame stub_frame = thread->last_frame();
2114   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
2115   frame caller_frame = stub_frame.sender(&reg_map);
2116   return caller_frame.is_deoptimized_frame();
2117 }
2118 
2119 static const TypeFunc* make_register_finalizer_Type() {
2120   // create input type (domain)
2121   const Type **fields = TypeTuple::fields(1);
2122   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
2123   // // The JavaThread* is passed to each routine as the last argument
2124   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
2125   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2126 
2127   // create result type (range)
2128   fields = TypeTuple::fields(0);
2129 
2130   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2131 
2132   return TypeFunc::make(domain,range);
2133 }
2134 
2135 #if INCLUDE_JFR
2136 static const TypeFunc* make_class_id_load_barrier_Type() {
2137   // create input type (domain)
2138   const Type **fields = TypeTuple::fields(1);
2139   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2140   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2141 
2142   // create result type (range)
2143   fields = TypeTuple::fields(0);
2144 
2145   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2146 
2147   return TypeFunc::make(domain,range);
2148 }
2149 #endif // INCLUDE_JFR
2150 
2151 //-----------------------------------------------------------------------------
2152 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2153   // create input type (domain)
2154   const Type **fields = TypeTuple::fields(2);
2155   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2156   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
2157   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2158 
2159   // create result type (range)
2160   fields = TypeTuple::fields(0);
2161 
2162   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2163 
2164   return TypeFunc::make(domain,range);
2165 }
2166 
2167 static const TypeFunc* make_dtrace_object_alloc_Type() {
2168   // create input type (domain)
2169   const Type **fields = TypeTuple::fields(2);
2170   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2171   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
2172 
2173   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2174 
2175   // create result type (range)
2176   fields = TypeTuple::fields(0);
2177 
2178   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2179 
2180   return TypeFunc::make(domain,range);
2181 }
2182 
2183 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2184   assert(oopDesc::is_oop(obj), "must be a valid oop");
2185   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2186   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2187 JRT_END
2188 
2189 //-----------------------------------------------------------------------------
2190 
2191 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2192 
2193 //
2194 // dump the collected NamedCounters.
2195 //
2196 void OptoRuntime::print_named_counters() {
2197   int total_lock_count = 0;
2198   int eliminated_lock_count = 0;
2199 
2200   NamedCounter* c = _named_counters;
2201   while (c) {
2202     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2203       int count = c->count();
2204       if (count > 0) {
2205         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2206         if (Verbose) {
2207           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2208         }
2209         total_lock_count += count;
2210         if (eliminated) {
2211           eliminated_lock_count += count;
2212         }
2213       }
2214     }
2215     c = c->next();
2216   }
2217   if (total_lock_count > 0) {
2218     tty->print_cr("dynamic locks: %d", total_lock_count);
2219     if (eliminated_lock_count) {
2220       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
2221                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
2222     }
2223   }
2224 }
2225 
2226 //
2227 //  Allocate a new NamedCounter.  The JVMState is used to generate the
2228 //  name which consists of method@line for the inlining tree.
2229 //
2230 
2231 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
2232   int max_depth = youngest_jvms->depth();
2233 
2234   // Visit scopes from youngest to oldest.
2235   bool first = true;
2236   stringStream st;
2237   for (int depth = max_depth; depth >= 1; depth--) {
2238     JVMState* jvms = youngest_jvms->of_depth(depth);
2239     ciMethod* m = jvms->has_method() ? jvms->method() : nullptr;
2240     if (!first) {
2241       st.print(" ");
2242     } else {
2243       first = false;
2244     }
2245     int bci = jvms->bci();
2246     if (bci < 0) bci = 0;
2247     if (m != nullptr) {
2248       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
2249     } else {
2250       st.print("no method");
2251     }
2252     st.print("@%d", bci);
2253     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
2254   }
2255   NamedCounter* c = new NamedCounter(st.freeze(), tag);
2256 
2257   // atomically add the new counter to the head of the list.  We only
2258   // add counters so this is safe.
2259   NamedCounter* head;
2260   do {
2261     c->set_next(nullptr);
2262     head = _named_counters;
2263     c->set_next(head);
2264   } while (AtomicAccess::cmpxchg(&_named_counters, head, c) != head);
2265   return c;
2266 }
2267 
2268 void OptoRuntime::initialize_types() {
2269   _new_instance_Type                  = make_new_instance_Type();
2270   _new_array_Type                     = make_new_array_Type();
2271   _multianewarray2_Type               = multianewarray_Type(2);
2272   _multianewarray3_Type               = multianewarray_Type(3);
2273   _multianewarray4_Type               = multianewarray_Type(4);
2274   _multianewarray5_Type               = multianewarray_Type(5);
2275   _multianewarrayN_Type               = make_multianewarrayN_Type();
2276   _complete_monitor_enter_Type        = make_complete_monitor_enter_Type();
2277   _complete_monitor_exit_Type         = make_complete_monitor_exit_Type();
2278   _monitor_notify_Type                = make_monitor_notify_Type();
2279   _uncommon_trap_Type                 = make_uncommon_trap_Type();
2280   _athrow_Type                        = make_athrow_Type();
2281   _rethrow_Type                       = make_rethrow_Type();
2282   _Math_D_D_Type                      = make_Math_D_D_Type();
2283   _Math_DD_D_Type                     = make_Math_DD_D_Type();
2284   _modf_Type                          = make_modf_Type();
2285   _l2f_Type                           = make_l2f_Type();
2286   _void_long_Type                     = make_void_long_Type();
2287   _void_void_Type                     = make_void_void_Type();
2288   _jfr_write_checkpoint_Type          = make_jfr_write_checkpoint_Type();
2289   _flush_windows_Type                 = make_flush_windows_Type();
2290   _fast_arraycopy_Type                = make_arraycopy_Type(ac_fast);
2291   _checkcast_arraycopy_Type           = make_arraycopy_Type(ac_checkcast);
2292   _generic_arraycopy_Type             = make_arraycopy_Type(ac_generic);
2293   _slow_arraycopy_Type                = make_arraycopy_Type(ac_slow);
2294   _unsafe_setmemory_Type              = make_setmemory_Type();
2295   _array_fill_Type                    = make_array_fill_Type();
2296   _array_sort_Type                    = make_array_sort_Type();
2297   _array_partition_Type               = make_array_partition_Type();
2298   _aescrypt_block_Type                = make_aescrypt_block_Type();
2299   _cipherBlockChaining_aescrypt_Type  = make_cipherBlockChaining_aescrypt_Type();
2300   _electronicCodeBook_aescrypt_Type   = make_electronicCodeBook_aescrypt_Type();
2301   _counterMode_aescrypt_Type          = make_counterMode_aescrypt_Type();
2302   _galoisCounterMode_aescrypt_Type    = make_galoisCounterMode_aescrypt_Type();
2303   _digestBase_implCompress_with_sha3_Type      = make_digestBase_implCompress_Type(  /* is_sha3= */ true);
2304   _digestBase_implCompress_without_sha3_Type   = make_digestBase_implCompress_Type(  /* is_sha3= */ false);;
2305   _digestBase_implCompressMB_with_sha3_Type    = make_digestBase_implCompressMB_Type(/* is_sha3= */ true);
2306   _digestBase_implCompressMB_without_sha3_Type = make_digestBase_implCompressMB_Type(/* is_sha3= */ false);
2307   _double_keccak_Type                 = make_double_keccak_Type();
2308   _multiplyToLen_Type                 = make_multiplyToLen_Type();
2309   _montgomeryMultiply_Type            = make_montgomeryMultiply_Type();
2310   _montgomerySquare_Type              = make_montgomerySquare_Type();
2311   _squareToLen_Type                   = make_squareToLen_Type();
2312   _mulAdd_Type                        = make_mulAdd_Type();
2313   _bigIntegerShift_Type               = make_bigIntegerShift_Type();
2314   _vectorizedMismatch_Type            = make_vectorizedMismatch_Type();
2315   _ghash_processBlocks_Type           = make_ghash_processBlocks_Type();
2316   _chacha20Block_Type                 = make_chacha20Block_Type();
2317   _kyberNtt_Type                      = make_kyberNtt_Type();
2318   _kyberInverseNtt_Type               = make_kyberInverseNtt_Type();
2319   _kyberNttMult_Type                  = make_kyberNttMult_Type();
2320   _kyberAddPoly_2_Type                = make_kyberAddPoly_2_Type();
2321   _kyberAddPoly_3_Type                = make_kyberAddPoly_3_Type();
2322   _kyber12To16_Type                   = make_kyber12To16_Type();
2323   _kyberBarrettReduce_Type            = make_kyberBarrettReduce_Type();
2324   _dilithiumAlmostNtt_Type            = make_dilithiumAlmostNtt_Type();
2325   _dilithiumAlmostInverseNtt_Type     = make_dilithiumAlmostInverseNtt_Type();
2326   _dilithiumNttMult_Type              = make_dilithiumNttMult_Type();
2327   _dilithiumMontMulByConstant_Type    = make_dilithiumMontMulByConstant_Type();
2328   _dilithiumDecomposePoly_Type        = make_dilithiumDecomposePoly_Type();
2329   _base64_encodeBlock_Type            = make_base64_encodeBlock_Type();
2330   _base64_decodeBlock_Type            = make_base64_decodeBlock_Type();
2331   _string_IndexOf_Type                = make_string_IndexOf_Type();
2332   _poly1305_processBlocks_Type        = make_poly1305_processBlocks_Type();
2333   _intpoly_montgomeryMult_P256_Type   = make_intpoly_montgomeryMult_P256_Type();
2334   _intpoly_assign_Type                = make_intpoly_assign_Type();
2335   _updateBytesCRC32_Type              = make_updateBytesCRC32_Type();
2336   _updateBytesCRC32C_Type             = make_updateBytesCRC32C_Type();
2337   _updateBytesAdler32_Type            = make_updateBytesAdler32_Type();
2338   _osr_end_Type                       = make_osr_end_Type();
2339   _register_finalizer_Type            = make_register_finalizer_Type();
2340   _vthread_transition_Type            = make_vthread_transition_Type();
2341   JFR_ONLY(
2342     _class_id_load_barrier_Type       = make_class_id_load_barrier_Type();
2343   )
2344   _dtrace_method_entry_exit_Type      = make_dtrace_method_entry_exit_Type();
2345   _dtrace_object_alloc_Type           = make_dtrace_object_alloc_Type();
2346 }
2347 
2348 int trace_exception_counter = 0;
2349 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2350   trace_exception_counter++;
2351   stringStream tempst;
2352 
2353   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2354   exception_oop->print_value_on(&tempst);
2355   tempst.print(" in ");
2356   CodeBlob* blob = CodeCache::find_blob(exception_pc);
2357   if (blob->is_nmethod()) {
2358     blob->as_nmethod()->method()->print_value_on(&tempst);
2359   } else if (blob->is_runtime_stub()) {
2360     tempst.print("<runtime-stub>");
2361   } else {
2362     tempst.print("<unknown>");
2363   }
2364   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
2365   tempst.print("]");
2366 
2367   st->print_raw_cr(tempst.freeze());
2368 }