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