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