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