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