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