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