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      = 6;
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   fields[argp++] = TypeInt::INT;        // zlen
1163   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1164   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1165 
1166   // no result type needed
1167   fields = TypeTuple::fields(1);
1168   fields[TypeFunc::Parms+0] = nullptr;
1169   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1170   return TypeFunc::make(domain, range);
1171 }
1172 
1173 const TypeFunc* OptoRuntime::squareToLen_Type() {
1174   // create input type (domain)
1175   int num_args      = 4;
1176   int argcnt = num_args;
1177   const Type** fields = TypeTuple::fields(argcnt);
1178   int argp = TypeFunc::Parms;
1179   fields[argp++] = TypePtr::NOTNULL;    // x
1180   fields[argp++] = TypeInt::INT;        // len
1181   fields[argp++] = TypePtr::NOTNULL;    // z
1182   fields[argp++] = TypeInt::INT;        // zlen
1183   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1184   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1185 
1186   // no result type needed
1187   fields = TypeTuple::fields(1);
1188   fields[TypeFunc::Parms+0] = nullptr;
1189   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1190   return TypeFunc::make(domain, range);
1191 }
1192 
1193 // for mulAdd calls, 2 pointers and 3 ints, returning int
1194 const TypeFunc* OptoRuntime::mulAdd_Type() {
1195   // create input type (domain)
1196   int num_args      = 5;
1197   int argcnt = num_args;
1198   const Type** fields = TypeTuple::fields(argcnt);
1199   int argp = TypeFunc::Parms;
1200   fields[argp++] = TypePtr::NOTNULL;    // out
1201   fields[argp++] = TypePtr::NOTNULL;    // in
1202   fields[argp++] = TypeInt::INT;        // offset
1203   fields[argp++] = TypeInt::INT;        // len
1204   fields[argp++] = TypeInt::INT;        // k
1205   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1206   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1207 
1208   // returning carry (int)
1209   fields = TypeTuple::fields(1);
1210   fields[TypeFunc::Parms+0] = TypeInt::INT;
1211   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1212   return TypeFunc::make(domain, range);
1213 }
1214 
1215 const TypeFunc* OptoRuntime::montgomeryMultiply_Type() {
1216   // create input type (domain)
1217   int num_args      = 7;
1218   int argcnt = num_args;
1219   const Type** fields = TypeTuple::fields(argcnt);
1220   int argp = TypeFunc::Parms;
1221   fields[argp++] = TypePtr::NOTNULL;    // a
1222   fields[argp++] = TypePtr::NOTNULL;    // b
1223   fields[argp++] = TypePtr::NOTNULL;    // n
1224   fields[argp++] = TypeInt::INT;        // len
1225   fields[argp++] = TypeLong::LONG;      // inv
1226   fields[argp++] = Type::HALF;
1227   fields[argp++] = TypePtr::NOTNULL;    // result
1228   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1229   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1230 
1231   // result type needed
1232   fields = TypeTuple::fields(1);
1233   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1234 
1235   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1236   return TypeFunc::make(domain, range);
1237 }
1238 
1239 const TypeFunc* OptoRuntime::montgomerySquare_Type() {
1240   // create input type (domain)
1241   int num_args      = 6;
1242   int argcnt = num_args;
1243   const Type** fields = TypeTuple::fields(argcnt);
1244   int argp = TypeFunc::Parms;
1245   fields[argp++] = TypePtr::NOTNULL;    // a
1246   fields[argp++] = TypePtr::NOTNULL;    // n
1247   fields[argp++] = TypeInt::INT;        // len
1248   fields[argp++] = TypeLong::LONG;      // inv
1249   fields[argp++] = Type::HALF;
1250   fields[argp++] = TypePtr::NOTNULL;    // result
1251   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1252   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1253 
1254   // result type needed
1255   fields = TypeTuple::fields(1);
1256   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1257 
1258   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1259   return TypeFunc::make(domain, range);
1260 }
1261 
1262 const TypeFunc * OptoRuntime::bigIntegerShift_Type() {
1263   int argcnt = 5;
1264   const Type** fields = TypeTuple::fields(argcnt);
1265   int argp = TypeFunc::Parms;
1266   fields[argp++] = TypePtr::NOTNULL;    // newArr
1267   fields[argp++] = TypePtr::NOTNULL;    // oldArr
1268   fields[argp++] = TypeInt::INT;        // newIdx
1269   fields[argp++] = TypeInt::INT;        // shiftCount
1270   fields[argp++] = TypeInt::INT;        // numIter
1271   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1272   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1273 
1274   // no result type needed
1275   fields = TypeTuple::fields(1);
1276   fields[TypeFunc::Parms + 0] = nullptr;
1277   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1278   return TypeFunc::make(domain, range);
1279 }
1280 
1281 const TypeFunc* OptoRuntime::vectorizedMismatch_Type() {
1282   // create input type (domain)
1283   int num_args = 4;
1284   int argcnt = num_args;
1285   const Type** fields = TypeTuple::fields(argcnt);
1286   int argp = TypeFunc::Parms;
1287   fields[argp++] = TypePtr::NOTNULL;    // obja
1288   fields[argp++] = TypePtr::NOTNULL;    // objb
1289   fields[argp++] = TypeInt::INT;        // length, number of elements
1290   fields[argp++] = TypeInt::INT;        // log2scale, element size
1291   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1292   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1293 
1294   //return mismatch index (int)
1295   fields = TypeTuple::fields(1);
1296   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1297   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1298   return TypeFunc::make(domain, range);
1299 }
1300 
1301 // GHASH block processing
1302 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1303     int argcnt = 4;
1304 
1305     const Type** fields = TypeTuple::fields(argcnt);
1306     int argp = TypeFunc::Parms;
1307     fields[argp++] = TypePtr::NOTNULL;    // state
1308     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1309     fields[argp++] = TypePtr::NOTNULL;    // data
1310     fields[argp++] = TypeInt::INT;        // blocks
1311     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1312     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1313 
1314     // result type needed
1315     fields = TypeTuple::fields(1);
1316     fields[TypeFunc::Parms+0] = nullptr; // void
1317     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1318     return TypeFunc::make(domain, range);
1319 }
1320 
1321 // ChaCha20 Block function
1322 const TypeFunc* OptoRuntime::chacha20Block_Type() {
1323     int argcnt = 2;
1324 
1325     const Type** fields = TypeTuple::fields(argcnt);
1326     int argp = TypeFunc::Parms;
1327     fields[argp++] = TypePtr::NOTNULL;      // state
1328     fields[argp++] = TypePtr::NOTNULL;      // result
1329 
1330     assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1331     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1332 
1333     // result type needed
1334     fields = TypeTuple::fields(1);
1335     fields[TypeFunc::Parms + 0] = TypeInt::INT;     // key stream outlen as int
1336     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1337     return TypeFunc::make(domain, range);
1338 }
1339 
1340 // Base64 encode function
1341 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1342   int argcnt = 6;
1343 
1344   const Type** fields = TypeTuple::fields(argcnt);
1345   int argp = TypeFunc::Parms;
1346   fields[argp++] = TypePtr::NOTNULL;    // src array
1347   fields[argp++] = TypeInt::INT;        // offset
1348   fields[argp++] = TypeInt::INT;        // length
1349   fields[argp++] = TypePtr::NOTNULL;    // dest array
1350   fields[argp++] = TypeInt::INT;       // dp
1351   fields[argp++] = TypeInt::BOOL;       // isURL
1352   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1353   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1354 
1355   // result type needed
1356   fields = TypeTuple::fields(1);
1357   fields[TypeFunc::Parms + 0] = nullptr; // void
1358   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1359   return TypeFunc::make(domain, range);
1360 }
1361 // Base64 decode function
1362 const TypeFunc* OptoRuntime::base64_decodeBlock_Type() {
1363   int argcnt = 7;
1364 
1365   const Type** fields = TypeTuple::fields(argcnt);
1366   int argp = TypeFunc::Parms;
1367   fields[argp++] = TypePtr::NOTNULL;    // src array
1368   fields[argp++] = TypeInt::INT;        // src offset
1369   fields[argp++] = TypeInt::INT;        // src length
1370   fields[argp++] = TypePtr::NOTNULL;    // dest array
1371   fields[argp++] = TypeInt::INT;        // dest offset
1372   fields[argp++] = TypeInt::BOOL;       // isURL
1373   fields[argp++] = TypeInt::BOOL;       // isMIME
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; // count of bytes written to dst
1380   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1381   return TypeFunc::make(domain, range);
1382 }
1383 
1384 // Poly1305 processMultipleBlocks function
1385 const TypeFunc* OptoRuntime::poly1305_processBlocks_Type() {
1386   int argcnt = 4;
1387 
1388   const Type** fields = TypeTuple::fields(argcnt);
1389   int argp = TypeFunc::Parms;
1390   fields[argp++] = TypePtr::NOTNULL;    // input array
1391   fields[argp++] = TypeInt::INT;        // input length
1392   fields[argp++] = TypePtr::NOTNULL;    // accumulator array
1393   fields[argp++] = TypePtr::NOTNULL;    // r array
1394   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1395   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1396 
1397   // result type needed
1398   fields = TypeTuple::fields(1);
1399   fields[TypeFunc::Parms + 0] = nullptr; // void
1400   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1401   return TypeFunc::make(domain, range);
1402 }
1403 
1404 //------------- Interpreter state access for on stack replacement
1405 const TypeFunc* OptoRuntime::osr_end_Type() {
1406   // create input type (domain)
1407   const Type **fields = TypeTuple::fields(1);
1408   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1409   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1410 
1411   // create result type
1412   fields = TypeTuple::fields(1);
1413   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1414   fields[TypeFunc::Parms+0] = nullptr; // void
1415   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1416   return TypeFunc::make(domain, range);
1417 }
1418 
1419 //-------------------------------------------------------------------------------------
1420 // register policy
1421 
1422 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1423   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1424   switch (register_save_policy[reg]) {
1425     case 'C': return false; //SOC
1426     case 'E': return true ; //SOE
1427     case 'N': return false; //NS
1428     case 'A': return false; //AS
1429   }
1430   ShouldNotReachHere();
1431   return false;
1432 }
1433 
1434 //-----------------------------------------------------------------------
1435 // Exceptions
1436 //
1437 
1438 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1439 
1440 // The method is an entry that is always called by a C++ method not
1441 // directly from compiled code. Compiled code will call the C++ method following.
1442 // We can't allow async exception to be installed during  exception processing.
1443 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1444   // The frame we rethrow the exception to might not have been processed by the GC yet.
1445   // The stack watermark barrier takes care of detecting that and ensuring the frame
1446   // has updated oops.
1447   StackWatermarkSet::after_unwind(current);
1448 
1449   // Do not confuse exception_oop with pending_exception. The exception_oop
1450   // is only used to pass arguments into the method. Not for general
1451   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1452   // the runtime stubs checks this on exit.
1453   assert(current->exception_oop() != nullptr, "exception oop is found");
1454   address handler_address = nullptr;
1455 
1456   Handle exception(current, current->exception_oop());
1457   address pc = current->exception_pc();
1458 
1459   // Clear out the exception oop and pc since looking up an
1460   // exception handler can cause class loading, which might throw an
1461   // exception and those fields are expected to be clear during
1462   // normal bytecode execution.
1463   current->clear_exception_oop_and_pc();
1464 
1465   LogTarget(Info, exceptions) lt;
1466   if (lt.is_enabled()) {
1467     ResourceMark rm;
1468     LogStream ls(lt);
1469     trace_exception(&ls, exception(), pc, "");
1470   }
1471 
1472   // for AbortVMOnException flag
1473   Exceptions::debug_check_abort(exception);
1474 
1475 #ifdef ASSERT
1476   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1477     // should throw an exception here
1478     ShouldNotReachHere();
1479   }
1480 #endif
1481 
1482   // new exception handling: this method is entered only from adapters
1483   // exceptions from compiled java methods are handled in compiled code
1484   // using rethrow node
1485 
1486   nm = CodeCache::find_nmethod(pc);
1487   assert(nm != nullptr, "No NMethod found");
1488   if (nm->is_native_method()) {
1489     fatal("Native method should not have path to exception handling");
1490   } else {
1491     // we are switching to old paradigm: search for exception handler in caller_frame
1492     // instead in exception handler of caller_frame.sender()
1493 
1494     if (JvmtiExport::can_post_on_exceptions()) {
1495       // "Full-speed catching" is not necessary here,
1496       // since we're notifying the VM on every catch.
1497       // Force deoptimization and the rest of the lookup
1498       // will be fine.
1499       deoptimize_caller_frame(current);
1500     }
1501 
1502     // Check the stack guard pages.  If enabled, look for handler in this frame;
1503     // otherwise, forcibly unwind the frame.
1504     //
1505     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1506     bool force_unwind = !current->stack_overflow_state()->reguard_stack();
1507     bool deopting = false;
1508     if (nm->is_deopt_pc(pc)) {
1509       deopting = true;
1510       RegisterMap map(current,
1511                       RegisterMap::UpdateMap::skip,
1512                       RegisterMap::ProcessFrames::include,
1513                       RegisterMap::WalkContinuation::skip);
1514       frame deoptee = current->last_frame().sender(&map);
1515       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1516       // Adjust the pc back to the original throwing pc
1517       pc = deoptee.pc();
1518     }
1519 
1520     // If we are forcing an unwind because of stack overflow then deopt is
1521     // irrelevant since we are throwing the frame away anyway.
1522 
1523     if (deopting && !force_unwind) {
1524       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1525     } else {
1526 
1527       handler_address =
1528         force_unwind ? nullptr : nm->handler_for_exception_and_pc(exception, pc);
1529 
1530       if (handler_address == nullptr) {
1531         bool recursive_exception = false;
1532         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1533         assert (handler_address != nullptr, "must have compiled handler");
1534         // Update the exception cache only when the unwind was not forced
1535         // and there didn't happen another exception during the computation of the
1536         // compiled exception handler. Checking for exception oop equality is not
1537         // sufficient because some exceptions are pre-allocated and reused.
1538         if (!force_unwind && !recursive_exception) {
1539           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1540         }
1541       } else {
1542 #ifdef ASSERT
1543         bool recursive_exception = false;
1544         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1545         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1546                  p2i(handler_address), p2i(computed_address));
1547 #endif
1548       }
1549     }
1550 
1551     current->set_exception_pc(pc);
1552     current->set_exception_handler_pc(handler_address);
1553 
1554     // Check if the exception PC is a MethodHandle call site.
1555     current->set_is_method_handle_return(nm->is_method_handle_return(pc));
1556   }
1557 
1558   // Restore correct return pc.  Was saved above.
1559   current->set_exception_oop(exception());
1560   return handler_address;
1561 
1562 JRT_END
1563 
1564 // We are entering here from exception_blob
1565 // If there is a compiled exception handler in this method, we will continue there;
1566 // otherwise we will unwind the stack and continue at the caller of top frame method
1567 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1568 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1569 // we looked up the handler for has been deoptimized in the meantime. If it has been
1570 // we must not use the handler and instead return the deopt blob.
1571 address OptoRuntime::handle_exception_C(JavaThread* current) {
1572 //
1573 // We are in Java not VM and in debug mode we have a NoHandleMark
1574 //
1575 #ifndef PRODUCT
1576   SharedRuntime::_find_handler_ctr++;          // find exception handler
1577 #endif
1578   debug_only(NoHandleMark __hm;)
1579   nmethod* nm = nullptr;
1580   address handler_address = nullptr;
1581   {
1582     // Enter the VM
1583 
1584     ResetNoHandleMark rnhm;
1585     handler_address = handle_exception_C_helper(current, nm);
1586   }
1587 
1588   // Back in java: Use no oops, DON'T safepoint
1589 
1590   // Now check to see if the handler we are returning is in a now
1591   // deoptimized frame
1592 
1593   if (nm != nullptr) {
1594     RegisterMap map(current,
1595                     RegisterMap::UpdateMap::skip,
1596                     RegisterMap::ProcessFrames::skip,
1597                     RegisterMap::WalkContinuation::skip);
1598     frame caller = current->last_frame().sender(&map);
1599 #ifdef ASSERT
1600     assert(caller.is_compiled_frame(), "must be");
1601 #endif // ASSERT
1602     if (caller.is_deoptimized_frame()) {
1603       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1604     }
1605   }
1606   return handler_address;
1607 }
1608 
1609 //------------------------------rethrow----------------------------------------
1610 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1611 // is either throwing or rethrowing an exception.  The callee-save registers
1612 // have been restored, synchronized objects have been unlocked and the callee
1613 // stack frame has been removed.  The return address was passed in.
1614 // Exception oop is passed as the 1st argument.  This routine is then called
1615 // from the stub.  On exit, we know where to jump in the caller's code.
1616 // After this C code exits, the stub will pop his frame and end in a jump
1617 // (instead of a return).  We enter the caller's default handler.
1618 //
1619 // This must be JRT_LEAF:
1620 //     - caller will not change its state as we cannot block on exit,
1621 //       therefore raw_exception_handler_for_return_address is all it takes
1622 //       to handle deoptimized blobs
1623 //
1624 // However, there needs to be a safepoint check in the middle!  So compiled
1625 // safepoints are completely watertight.
1626 //
1627 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1628 //
1629 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1630 //
1631 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1632   // ret_pc will have been loaded from the stack, so for AArch64 will be signed.
1633   AARCH64_PORT_ONLY(ret_pc = pauth_strip_verifiable(ret_pc));
1634 
1635 #ifndef PRODUCT
1636   SharedRuntime::_rethrow_ctr++;               // count rethrows
1637 #endif
1638   assert (exception != nullptr, "should have thrown a NullPointerException");
1639 #ifdef ASSERT
1640   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1641     // should throw an exception here
1642     ShouldNotReachHere();
1643   }
1644 #endif
1645 
1646   thread->set_vm_result(exception);
1647   // Frame not compiled (handles deoptimization blob)
1648   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1649 }
1650 
1651 
1652 const TypeFunc *OptoRuntime::rethrow_Type() {
1653   // create input type (domain)
1654   const Type **fields = TypeTuple::fields(1);
1655   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1656   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1657 
1658   // create result type (range)
1659   fields = TypeTuple::fields(1);
1660   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1661   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1662 
1663   return TypeFunc::make(domain, range);
1664 }
1665 
1666 
1667 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1668   // Deoptimize the caller before continuing, as the compiled
1669   // exception handler table may not be valid.
1670   if (!StressCompiledExceptionHandlers && doit) {
1671     deoptimize_caller_frame(thread);
1672   }
1673 }
1674 
1675 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1676   // Called from within the owner thread, so no need for safepoint
1677   RegisterMap reg_map(thread,
1678                       RegisterMap::UpdateMap::include,
1679                       RegisterMap::ProcessFrames::include,
1680                       RegisterMap::WalkContinuation::skip);
1681   frame stub_frame = thread->last_frame();
1682   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1683   frame caller_frame = stub_frame.sender(&reg_map);
1684 
1685   // Deoptimize the caller frame.
1686   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1687 }
1688 
1689 
1690 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1691   // Called from within the owner thread, so no need for safepoint
1692   RegisterMap reg_map(thread,
1693                       RegisterMap::UpdateMap::include,
1694                       RegisterMap::ProcessFrames::include,
1695                       RegisterMap::WalkContinuation::skip);
1696   frame stub_frame = thread->last_frame();
1697   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1698   frame caller_frame = stub_frame.sender(&reg_map);
1699   return caller_frame.is_deoptimized_frame();
1700 }
1701 
1702 
1703 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1704   // create input type (domain)
1705   const Type **fields = TypeTuple::fields(1);
1706   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1707   // // The JavaThread* is passed to each routine as the last argument
1708   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1709   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1710 
1711   // create result type (range)
1712   fields = TypeTuple::fields(0);
1713 
1714   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1715 
1716   return TypeFunc::make(domain,range);
1717 }
1718 
1719 #if INCLUDE_JFR
1720 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1721   // create input type (domain)
1722   const Type **fields = TypeTuple::fields(1);
1723   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1724   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1725 
1726   // create result type (range)
1727   fields = TypeTuple::fields(0);
1728 
1729   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1730 
1731   return TypeFunc::make(domain,range);
1732 }
1733 #endif
1734 
1735 //-----------------------------------------------------------------------------
1736 // Dtrace support.  entry and exit probes have the same signature
1737 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1738   // create input type (domain)
1739   const Type **fields = TypeTuple::fields(2);
1740   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1741   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1742   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1743 
1744   // create result type (range)
1745   fields = TypeTuple::fields(0);
1746 
1747   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1748 
1749   return TypeFunc::make(domain,range);
1750 }
1751 
1752 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1753   // create input type (domain)
1754   const Type **fields = TypeTuple::fields(2);
1755   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1756   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1757 
1758   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1759 
1760   // create result type (range)
1761   fields = TypeTuple::fields(0);
1762 
1763   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1764 
1765   return TypeFunc::make(domain,range);
1766 }
1767 
1768 
1769 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1770   assert(oopDesc::is_oop(obj), "must be a valid oop");
1771   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1772   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1773 JRT_END
1774 
1775 //-----------------------------------------------------------------------------
1776 
1777 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1778 
1779 //
1780 // dump the collected NamedCounters.
1781 //
1782 void OptoRuntime::print_named_counters() {
1783   int total_lock_count = 0;
1784   int eliminated_lock_count = 0;
1785 
1786   NamedCounter* c = _named_counters;
1787   while (c) {
1788     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1789       int count = c->count();
1790       if (count > 0) {
1791         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1792         if (Verbose) {
1793           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1794         }
1795         total_lock_count += count;
1796         if (eliminated) {
1797           eliminated_lock_count += count;
1798         }
1799       }
1800 #if INCLUDE_RTM_OPT
1801     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1802       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1803       if (rlc->nonzero()) {
1804         tty->print_cr("%s", c->name());
1805         rlc->print_on(tty);
1806       }
1807 #endif
1808     }
1809     c = c->next();
1810   }
1811   if (total_lock_count > 0) {
1812     tty->print_cr("dynamic locks: %d", total_lock_count);
1813     if (eliminated_lock_count) {
1814       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1815                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1816     }
1817   }
1818 }
1819 
1820 //
1821 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1822 //  name which consists of method@line for the inlining tree.
1823 //
1824 
1825 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1826   int max_depth = youngest_jvms->depth();
1827 
1828   // Visit scopes from youngest to oldest.
1829   bool first = true;
1830   stringStream st;
1831   for (int depth = max_depth; depth >= 1; depth--) {
1832     JVMState* jvms = youngest_jvms->of_depth(depth);
1833     ciMethod* m = jvms->has_method() ? jvms->method() : nullptr;
1834     if (!first) {
1835       st.print(" ");
1836     } else {
1837       first = false;
1838     }
1839     int bci = jvms->bci();
1840     if (bci < 0) bci = 0;
1841     if (m != nullptr) {
1842       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1843     } else {
1844       st.print("no method");
1845     }
1846     st.print("@%d", bci);
1847     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1848   }
1849   NamedCounter* c;
1850   if (tag == NamedCounter::RTMLockingCounter) {
1851     c = new RTMLockingNamedCounter(st.freeze());
1852   } else {
1853     c = new NamedCounter(st.freeze(), tag);
1854   }
1855 
1856   // atomically add the new counter to the head of the list.  We only
1857   // add counters so this is safe.
1858   NamedCounter* head;
1859   do {
1860     c->set_next(nullptr);
1861     head = _named_counters;
1862     c->set_next(head);
1863   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1864   return c;
1865 }
1866 
1867 int trace_exception_counter = 0;
1868 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1869   trace_exception_counter++;
1870   stringStream tempst;
1871 
1872   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1873   exception_oop->print_value_on(&tempst);
1874   tempst.print(" in ");
1875   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1876   if (blob->is_nmethod()) {
1877     blob->as_nmethod()->method()->print_value_on(&tempst);
1878   } else if (blob->is_runtime_stub()) {
1879     tempst.print("<runtime-stub>");
1880   } else {
1881     tempst.print("<unknown>");
1882   }
1883   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1884   tempst.print("]");
1885 
1886   st->print_raw_cr(tempst.freeze());
1887 }