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