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