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