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