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