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