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