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