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