1 /*
   2  * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledMethod.inline.hpp"
  30 #include "code/compiledIC.hpp"
  31 #include "code/icBuffer.hpp"
  32 #include "code/nmethod.hpp"
  33 #include "code/pcDesc.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/oopMap.hpp"
  38 #include "gc/g1/heapRegion.hpp"
  39 #include "gc/shared/barrierSet.hpp"
  40 #include "gc/shared/collectedHeap.hpp"
  41 #include "gc/shared/gcLocker.hpp"
  42 #include "interpreter/bytecode.hpp"
  43 #include "interpreter/interpreter.hpp"
  44 #include "interpreter/linkResolver.hpp"
  45 #include "logging/log.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "oops/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_flatArray(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     int 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 = 9;
 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; // long[] avx512_subkeyHtbl newly created
 980   fields[argp++] = TypePtr::NOTNULL; // byte[] counter from GCTR obj
 981 
 982   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
 983   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
 984   // returning cipher len (int)
 985   fields = TypeTuple::fields(1);
 986   fields[TypeFunc::Parms + 0] = TypeInt::INT;
 987   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
 988   return TypeFunc::make(domain, range);
 989 }
 990 
 991 /*
 992  * void implCompress(byte[] buf, int ofs)
 993  */
 994 const TypeFunc* OptoRuntime::digestBase_implCompress_Type(bool is_sha3) {
 995   // create input type (domain)
 996   int num_args = is_sha3 ? 3 : 2;
 997   int argcnt = num_args;
 998   const Type** fields = TypeTuple::fields(argcnt);
 999   int argp = TypeFunc::Parms;
1000   fields[argp++] = TypePtr::NOTNULL; // buf
1001   fields[argp++] = TypePtr::NOTNULL; // state
1002   if (is_sha3) fields[argp++] = TypeInt::INT; // digest_length
1003   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1004   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1005 
1006   // no result type needed
1007   fields = TypeTuple::fields(1);
1008   fields[TypeFunc::Parms+0] = NULL; // void
1009   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1010   return TypeFunc::make(domain, range);
1011 }
1012 
1013 /*
1014  * int implCompressMultiBlock(byte[] b, int ofs, int limit)
1015  */
1016 const TypeFunc* OptoRuntime::digestBase_implCompressMB_Type(bool is_sha3) {
1017   // create input type (domain)
1018   int num_args = is_sha3 ? 5 : 4;
1019   int argcnt = num_args;
1020   const Type** fields = TypeTuple::fields(argcnt);
1021   int argp = TypeFunc::Parms;
1022   fields[argp++] = TypePtr::NOTNULL; // buf
1023   fields[argp++] = TypePtr::NOTNULL; // state
1024   if (is_sha3) fields[argp++] = TypeInt::INT; // digest_length
1025   fields[argp++] = TypeInt::INT;     // ofs
1026   fields[argp++] = TypeInt::INT;     // limit
1027   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1028   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1029 
1030   // returning ofs (int)
1031   fields = TypeTuple::fields(1);
1032   fields[TypeFunc::Parms+0] = TypeInt::INT; // ofs
1033   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1034   return TypeFunc::make(domain, range);
1035 }
1036 
1037 const TypeFunc* OptoRuntime::multiplyToLen_Type() {
1038   // create input type (domain)
1039   int num_args      = 6;
1040   int argcnt = num_args;
1041   const Type** fields = TypeTuple::fields(argcnt);
1042   int argp = TypeFunc::Parms;
1043   fields[argp++] = TypePtr::NOTNULL;    // x
1044   fields[argp++] = TypeInt::INT;        // xlen
1045   fields[argp++] = TypePtr::NOTNULL;    // y
1046   fields[argp++] = TypeInt::INT;        // ylen
1047   fields[argp++] = TypePtr::NOTNULL;    // z
1048   fields[argp++] = TypeInt::INT;        // zlen
1049   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1050   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1051 
1052   // no result type needed
1053   fields = TypeTuple::fields(1);
1054   fields[TypeFunc::Parms+0] = NULL;
1055   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1056   return TypeFunc::make(domain, range);
1057 }
1058 
1059 const TypeFunc* OptoRuntime::squareToLen_Type() {
1060   // create input type (domain)
1061   int num_args      = 4;
1062   int argcnt = num_args;
1063   const Type** fields = TypeTuple::fields(argcnt);
1064   int argp = TypeFunc::Parms;
1065   fields[argp++] = TypePtr::NOTNULL;    // x
1066   fields[argp++] = TypeInt::INT;        // len
1067   fields[argp++] = TypePtr::NOTNULL;    // z
1068   fields[argp++] = TypeInt::INT;        // zlen
1069   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1070   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1071 
1072   // no result type needed
1073   fields = TypeTuple::fields(1);
1074   fields[TypeFunc::Parms+0] = NULL;
1075   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1076   return TypeFunc::make(domain, range);
1077 }
1078 
1079 // for mulAdd calls, 2 pointers and 3 ints, returning int
1080 const TypeFunc* OptoRuntime::mulAdd_Type() {
1081   // create input type (domain)
1082   int num_args      = 5;
1083   int argcnt = num_args;
1084   const Type** fields = TypeTuple::fields(argcnt);
1085   int argp = TypeFunc::Parms;
1086   fields[argp++] = TypePtr::NOTNULL;    // out
1087   fields[argp++] = TypePtr::NOTNULL;    // in
1088   fields[argp++] = TypeInt::INT;        // offset
1089   fields[argp++] = TypeInt::INT;        // len
1090   fields[argp++] = TypeInt::INT;        // k
1091   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1092   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1093 
1094   // returning carry (int)
1095   fields = TypeTuple::fields(1);
1096   fields[TypeFunc::Parms+0] = TypeInt::INT;
1097   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1098   return TypeFunc::make(domain, range);
1099 }
1100 
1101 const TypeFunc* OptoRuntime::montgomeryMultiply_Type() {
1102   // create input type (domain)
1103   int num_args      = 7;
1104   int argcnt = num_args;
1105   const Type** fields = TypeTuple::fields(argcnt);
1106   int argp = TypeFunc::Parms;
1107   fields[argp++] = TypePtr::NOTNULL;    // a
1108   fields[argp++] = TypePtr::NOTNULL;    // b
1109   fields[argp++] = TypePtr::NOTNULL;    // n
1110   fields[argp++] = TypeInt::INT;        // len
1111   fields[argp++] = TypeLong::LONG;      // inv
1112   fields[argp++] = Type::HALF;
1113   fields[argp++] = TypePtr::NOTNULL;    // result
1114   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1115   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1116 
1117   // result type needed
1118   fields = TypeTuple::fields(1);
1119   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1120 
1121   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1122   return TypeFunc::make(domain, range);
1123 }
1124 
1125 const TypeFunc* OptoRuntime::montgomerySquare_Type() {
1126   // create input type (domain)
1127   int num_args      = 6;
1128   int argcnt = num_args;
1129   const Type** fields = TypeTuple::fields(argcnt);
1130   int argp = TypeFunc::Parms;
1131   fields[argp++] = TypePtr::NOTNULL;    // a
1132   fields[argp++] = TypePtr::NOTNULL;    // n
1133   fields[argp++] = TypeInt::INT;        // len
1134   fields[argp++] = TypeLong::LONG;      // inv
1135   fields[argp++] = Type::HALF;
1136   fields[argp++] = TypePtr::NOTNULL;    // result
1137   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1138   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1139 
1140   // result type needed
1141   fields = TypeTuple::fields(1);
1142   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1143 
1144   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1145   return TypeFunc::make(domain, range);
1146 }
1147 
1148 const TypeFunc * OptoRuntime::bigIntegerShift_Type() {
1149   int argcnt = 5;
1150   const Type** fields = TypeTuple::fields(argcnt);
1151   int argp = TypeFunc::Parms;
1152   fields[argp++] = TypePtr::NOTNULL;    // newArr
1153   fields[argp++] = TypePtr::NOTNULL;    // oldArr
1154   fields[argp++] = TypeInt::INT;        // newIdx
1155   fields[argp++] = TypeInt::INT;        // shiftCount
1156   fields[argp++] = TypeInt::INT;        // numIter
1157   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1158   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1159 
1160   // no result type needed
1161   fields = TypeTuple::fields(1);
1162   fields[TypeFunc::Parms + 0] = NULL;
1163   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1164   return TypeFunc::make(domain, range);
1165 }
1166 
1167 const TypeFunc* OptoRuntime::vectorizedMismatch_Type() {
1168   // create input type (domain)
1169   int num_args = 4;
1170   int argcnt = num_args;
1171   const Type** fields = TypeTuple::fields(argcnt);
1172   int argp = TypeFunc::Parms;
1173   fields[argp++] = TypePtr::NOTNULL;    // obja
1174   fields[argp++] = TypePtr::NOTNULL;    // objb
1175   fields[argp++] = TypeInt::INT;        // length, number of elements
1176   fields[argp++] = TypeInt::INT;        // log2scale, element size
1177   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1178   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1179 
1180   //return mismatch index (int)
1181   fields = TypeTuple::fields(1);
1182   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1183   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1184   return TypeFunc::make(domain, range);
1185 }
1186 
1187 // GHASH block processing
1188 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1189     int argcnt = 4;
1190 
1191     const Type** fields = TypeTuple::fields(argcnt);
1192     int argp = TypeFunc::Parms;
1193     fields[argp++] = TypePtr::NOTNULL;    // state
1194     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1195     fields[argp++] = TypePtr::NOTNULL;    // data
1196     fields[argp++] = TypeInt::INT;        // blocks
1197     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1198     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1199 
1200     // result type needed
1201     fields = TypeTuple::fields(1);
1202     fields[TypeFunc::Parms+0] = NULL; // void
1203     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1204     return TypeFunc::make(domain, range);
1205 }
1206 // Base64 encode function
1207 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1208   int argcnt = 6;
1209 
1210   const Type** fields = TypeTuple::fields(argcnt);
1211   int argp = TypeFunc::Parms;
1212   fields[argp++] = TypePtr::NOTNULL;    // src array
1213   fields[argp++] = TypeInt::INT;        // offset
1214   fields[argp++] = TypeInt::INT;        // length
1215   fields[argp++] = TypePtr::NOTNULL;    // dest array
1216   fields[argp++] = TypeInt::INT;       // dp
1217   fields[argp++] = TypeInt::BOOL;       // isURL
1218   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1219   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1220 
1221   // result type needed
1222   fields = TypeTuple::fields(1);
1223   fields[TypeFunc::Parms + 0] = NULL; // void
1224   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1225   return TypeFunc::make(domain, range);
1226 }
1227 // Base64 decode function
1228 const TypeFunc* OptoRuntime::base64_decodeBlock_Type() {
1229   int argcnt = 7;
1230 
1231   const Type** fields = TypeTuple::fields(argcnt);
1232   int argp = TypeFunc::Parms;
1233   fields[argp++] = TypePtr::NOTNULL;    // src array
1234   fields[argp++] = TypeInt::INT;        // src offset
1235   fields[argp++] = TypeInt::INT;        // src length
1236   fields[argp++] = TypePtr::NOTNULL;    // dest array
1237   fields[argp++] = TypeInt::INT;        // dest offset
1238   fields[argp++] = TypeInt::BOOL;       // isURL
1239   fields[argp++] = TypeInt::BOOL;       // isMIME
1240   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1241   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1242 
1243   // result type needed
1244   fields = TypeTuple::fields(1);
1245   fields[TypeFunc::Parms + 0] = TypeInt::INT; // count of bytes written to dst
1246   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1247   return TypeFunc::make(domain, range);
1248 }
1249 
1250 //------------- Interpreter state access for on stack replacement
1251 const TypeFunc* OptoRuntime::osr_end_Type() {
1252   // create input type (domain)
1253   const Type **fields = TypeTuple::fields(1);
1254   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1255   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1256 
1257   // create result type
1258   fields = TypeTuple::fields(1);
1259   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1260   fields[TypeFunc::Parms+0] = NULL; // void
1261   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1262   return TypeFunc::make(domain, range);
1263 }
1264 
1265 //-------------------------------------------------------------------------------------
1266 // register policy
1267 
1268 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1269   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1270   switch (register_save_policy[reg]) {
1271     case 'C': return false; //SOC
1272     case 'E': return true ; //SOE
1273     case 'N': return false; //NS
1274     case 'A': return false; //AS
1275   }
1276   ShouldNotReachHere();
1277   return false;
1278 }
1279 
1280 //-----------------------------------------------------------------------
1281 // Exceptions
1282 //
1283 
1284 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1285 
1286 // The method is an entry that is always called by a C++ method not
1287 // directly from compiled code. Compiled code will call the C++ method following.
1288 // We can't allow async exception to be installed during  exception processing.
1289 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1290   // Do not confuse exception_oop with pending_exception. The exception_oop
1291   // is only used to pass arguments into the method. Not for general
1292   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1293   // the runtime stubs checks this on exit.
1294   assert(current->exception_oop() != NULL, "exception oop is found");
1295   address handler_address = NULL;
1296 
1297   Handle exception(current, current->exception_oop());
1298   address pc = current->exception_pc();
1299 
1300   // Clear out the exception oop and pc since looking up an
1301   // exception handler can cause class loading, which might throw an
1302   // exception and those fields are expected to be clear during
1303   // normal bytecode execution.
1304   current->clear_exception_oop_and_pc();
1305 
1306   LogTarget(Info, exceptions) lt;
1307   if (lt.is_enabled()) {
1308     ResourceMark rm;
1309     LogStream ls(lt);
1310     trace_exception(&ls, exception(), pc, "");
1311   }
1312 
1313   // for AbortVMOnException flag
1314   Exceptions::debug_check_abort(exception);
1315 
1316 #ifdef ASSERT
1317   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1318     // should throw an exception here
1319     ShouldNotReachHere();
1320   }
1321 #endif
1322 
1323   // new exception handling: this method is entered only from adapters
1324   // exceptions from compiled java methods are handled in compiled code
1325   // using rethrow node
1326 
1327   nm = CodeCache::find_nmethod(pc);
1328   assert(nm != NULL, "No NMethod found");
1329   if (nm->is_native_method()) {
1330     fatal("Native method should not have path to exception handling");
1331   } else {
1332     // we are switching to old paradigm: search for exception handler in caller_frame
1333     // instead in exception handler of caller_frame.sender()
1334 
1335     if (JvmtiExport::can_post_on_exceptions()) {
1336       // "Full-speed catching" is not necessary here,
1337       // since we're notifying the VM on every catch.
1338       // Force deoptimization and the rest of the lookup
1339       // will be fine.
1340       deoptimize_caller_frame(current);
1341     }
1342 
1343     // Check the stack guard pages.  If enabled, look for handler in this frame;
1344     // otherwise, forcibly unwind the frame.
1345     //
1346     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1347     bool force_unwind = !current->stack_overflow_state()->reguard_stack();
1348     bool deopting = false;
1349     if (nm->is_deopt_pc(pc)) {
1350       deopting = true;
1351       RegisterMap map(current, false);
1352       frame deoptee = current->last_frame().sender(&map);
1353       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1354       // Adjust the pc back to the original throwing pc
1355       pc = deoptee.pc();
1356     }
1357 
1358     // If we are forcing an unwind because of stack overflow then deopt is
1359     // irrelevant since we are throwing the frame away anyway.
1360 
1361     if (deopting && !force_unwind) {
1362       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1363     } else {
1364 
1365       handler_address =
1366         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
1367 
1368       if (handler_address == NULL) {
1369         bool recursive_exception = false;
1370         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1371         assert (handler_address != NULL, "must have compiled handler");
1372         // Update the exception cache only when the unwind was not forced
1373         // and there didn't happen another exception during the computation of the
1374         // compiled exception handler. Checking for exception oop equality is not
1375         // sufficient because some exceptions are pre-allocated and reused.
1376         if (!force_unwind && !recursive_exception) {
1377           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1378         }
1379       } else {
1380 #ifdef ASSERT
1381         bool recursive_exception = false;
1382         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1383         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1384                  p2i(handler_address), p2i(computed_address));
1385 #endif
1386       }
1387     }
1388 
1389     current->set_exception_pc(pc);
1390     current->set_exception_handler_pc(handler_address);
1391 
1392     // Check if the exception PC is a MethodHandle call site.
1393     current->set_is_method_handle_return(nm->is_method_handle_return(pc));
1394   }
1395 
1396   // Restore correct return pc.  Was saved above.
1397   current->set_exception_oop(exception());
1398   return handler_address;
1399 
1400 JRT_END
1401 
1402 // We are entering here from exception_blob
1403 // If there is a compiled exception handler in this method, we will continue there;
1404 // otherwise we will unwind the stack and continue at the caller of top frame method
1405 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1406 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1407 // we looked up the handler for has been deoptimized in the meantime. If it has been
1408 // we must not use the handler and instead return the deopt blob.
1409 address OptoRuntime::handle_exception_C(JavaThread* current) {
1410 //
1411 // We are in Java not VM and in debug mode we have a NoHandleMark
1412 //
1413 #ifndef PRODUCT
1414   SharedRuntime::_find_handler_ctr++;          // find exception handler
1415 #endif
1416   debug_only(NoHandleMark __hm;)
1417   nmethod* nm = NULL;
1418   address handler_address = NULL;
1419   {
1420     // Enter the VM
1421 
1422     ResetNoHandleMark rnhm;
1423     handler_address = handle_exception_C_helper(current, nm);
1424   }
1425 
1426   // Back in java: Use no oops, DON'T safepoint
1427 
1428   // Now check to see if the handler we are returning is in a now
1429   // deoptimized frame
1430 
1431   if (nm != NULL) {
1432     RegisterMap map(current, false);
1433     frame caller = current->last_frame().sender(&map);
1434 #ifdef ASSERT
1435     assert(caller.is_compiled_frame(), "must be");
1436 #endif // ASSERT
1437     if (caller.is_deoptimized_frame()) {
1438       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1439     }
1440   }
1441   return handler_address;
1442 }
1443 
1444 //------------------------------rethrow----------------------------------------
1445 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1446 // is either throwing or rethrowing an exception.  The callee-save registers
1447 // have been restored, synchronized objects have been unlocked and the callee
1448 // stack frame has been removed.  The return address was passed in.
1449 // Exception oop is passed as the 1st argument.  This routine is then called
1450 // from the stub.  On exit, we know where to jump in the caller's code.
1451 // After this C code exits, the stub will pop his frame and end in a jump
1452 // (instead of a return).  We enter the caller's default handler.
1453 //
1454 // This must be JRT_LEAF:
1455 //     - caller will not change its state as we cannot block on exit,
1456 //       therefore raw_exception_handler_for_return_address is all it takes
1457 //       to handle deoptimized blobs
1458 //
1459 // However, there needs to be a safepoint check in the middle!  So compiled
1460 // safepoints are completely watertight.
1461 //
1462 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1463 //
1464 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1465 //
1466 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1467 
1468   // Enable WXWrite: the function called directly by compiled code.
1469   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
1470 
1471   // The frame we rethrow the exception to might not have been processed by the GC yet.
1472   // The stack watermark barrier takes care of detecting that and ensuring the frame
1473   // has updated oops.
1474   StackWatermarkSet::after_unwind(thread);
1475 
1476 #ifndef PRODUCT
1477   SharedRuntime::_rethrow_ctr++;               // count rethrows
1478 #endif
1479   assert (exception != NULL, "should have thrown a NULLPointerException");
1480 #ifdef ASSERT
1481   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1482     // should throw an exception here
1483     ShouldNotReachHere();
1484   }
1485 #endif
1486 
1487   thread->set_vm_result(exception);
1488   // Frame not compiled (handles deoptimization blob)
1489   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1490 }
1491 
1492 
1493 const TypeFunc *OptoRuntime::rethrow_Type() {
1494   // create input type (domain)
1495   const Type **fields = TypeTuple::fields(1);
1496   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1497   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1498 
1499   // create result type (range)
1500   fields = TypeTuple::fields(1);
1501   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1502   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1503 
1504   return TypeFunc::make(domain, range);
1505 }
1506 
1507 
1508 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1509   // Deoptimize the caller before continuing, as the compiled
1510   // exception handler table may not be valid.
1511   if (!StressCompiledExceptionHandlers && doit) {
1512     deoptimize_caller_frame(thread);
1513   }
1514 }
1515 
1516 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1517   // Called from within the owner thread, so no need for safepoint
1518   RegisterMap reg_map(thread);
1519   frame stub_frame = thread->last_frame();
1520   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1521   frame caller_frame = stub_frame.sender(&reg_map);
1522 
1523   // Deoptimize the caller frame.
1524   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1525 }
1526 
1527 
1528 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1529   // Called from within the owner thread, so no need for safepoint
1530   RegisterMap reg_map(thread);
1531   frame stub_frame = thread->last_frame();
1532   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1533   frame caller_frame = stub_frame.sender(&reg_map);
1534   return caller_frame.is_deoptimized_frame();
1535 }
1536 
1537 
1538 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1539   // create input type (domain)
1540   const Type **fields = TypeTuple::fields(1);
1541   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1542   // // The JavaThread* is passed to each routine as the last argument
1543   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1544   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1545 
1546   // create result type (range)
1547   fields = TypeTuple::fields(0);
1548 
1549   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1550 
1551   return TypeFunc::make(domain, range);
1552 }
1553 
1554 #if INCLUDE_JFR
1555 const TypeFunc *OptoRuntime::get_class_id_intrinsic_Type() {
1556   // create input type (domain)
1557   const Type **fields = TypeTuple::fields(1);
1558   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1559   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1560 
1561   // create result type (range)
1562   fields = TypeTuple::fields(0);
1563 
1564   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1565 
1566   return TypeFunc::make(domain,range);
1567 }
1568 #endif
1569 
1570 //-----------------------------------------------------------------------------
1571 // Dtrace support.  entry and exit probes have the same signature
1572 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1573   // create input type (domain)
1574   const Type **fields = TypeTuple::fields(2);
1575   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1576   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1577   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1578 
1579   // create result type (range)
1580   fields = TypeTuple::fields(0);
1581 
1582   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1583 
1584   return TypeFunc::make(domain, range);
1585 }
1586 
1587 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1588   // create input type (domain)
1589   const Type **fields = TypeTuple::fields(2);
1590   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1591   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1592 
1593   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1594 
1595   // create result type (range)
1596   fields = TypeTuple::fields(0);
1597 
1598   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1599 
1600   return TypeFunc::make(domain, range);
1601 }
1602 
1603 
1604 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1605   assert(oopDesc::is_oop(obj), "must be a valid oop");
1606   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1607   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1608 JRT_END
1609 
1610 //-----------------------------------------------------------------------------
1611 
1612 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1613 
1614 //
1615 // dump the collected NamedCounters.
1616 //
1617 void OptoRuntime::print_named_counters() {
1618   int total_lock_count = 0;
1619   int eliminated_lock_count = 0;
1620 
1621   NamedCounter* c = _named_counters;
1622   while (c) {
1623     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1624       int count = c->count();
1625       if (count > 0) {
1626         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1627         if (Verbose) {
1628           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1629         }
1630         total_lock_count += count;
1631         if (eliminated) {
1632           eliminated_lock_count += count;
1633         }
1634       }
1635 #if INCLUDE_RTM_OPT
1636     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1637       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1638       if (rlc->nonzero()) {
1639         tty->print_cr("%s", c->name());
1640         rlc->print_on(tty);
1641       }
1642 #endif
1643     }
1644     c = c->next();
1645   }
1646   if (total_lock_count > 0) {
1647     tty->print_cr("dynamic locks: %d", total_lock_count);
1648     if (eliminated_lock_count) {
1649       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1650                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1651     }
1652   }
1653 }
1654 
1655 //
1656 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1657 //  name which consists of method@line for the inlining tree.
1658 //
1659 
1660 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1661   int max_depth = youngest_jvms->depth();
1662 
1663   // Visit scopes from youngest to oldest.
1664   bool first = true;
1665   stringStream st;
1666   for (int depth = max_depth; depth >= 1; depth--) {
1667     JVMState* jvms = youngest_jvms->of_depth(depth);
1668     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1669     if (!first) {
1670       st.print(" ");
1671     } else {
1672       first = false;
1673     }
1674     int bci = jvms->bci();
1675     if (bci < 0) bci = 0;
1676     if (m != NULL) {
1677       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1678     } else {
1679       st.print("no method");
1680     }
1681     st.print("@%d", bci);
1682     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1683   }
1684   NamedCounter* c;
1685   if (tag == NamedCounter::RTMLockingCounter) {
1686     c = new RTMLockingNamedCounter(st.as_string());
1687   } else {
1688     c = new NamedCounter(st.as_string(), tag);
1689   }
1690 
1691   // atomically add the new counter to the head of the list.  We only
1692   // add counters so this is safe.
1693   NamedCounter* head;
1694   do {
1695     c->set_next(NULL);
1696     head = _named_counters;
1697     c->set_next(head);
1698   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1699   return c;
1700 }
1701 
1702 int trace_exception_counter = 0;
1703 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1704   trace_exception_counter++;
1705   stringStream tempst;
1706 
1707   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1708   exception_oop->print_value_on(&tempst);
1709   tempst.print(" in ");
1710   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1711   if (blob->is_compiled()) {
1712     CompiledMethod* cm = blob->as_compiled_method_or_null();
1713     cm->method()->print_value_on(&tempst);
1714   } else if (blob->is_runtime_stub()) {
1715     tempst.print("<runtime-stub>");
1716   } else {
1717     tempst.print("<unknown>");
1718   }
1719   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1720   tempst.print("]");
1721 
1722   st->print_raw_cr(tempst.as_string());
1723 }
1724 
1725 const TypeFunc *OptoRuntime::store_inline_type_fields_Type() {
1726   // create input type (domain)
1727   uint total = SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1728   const Type **fields = TypeTuple::fields(total);
1729   // We don't know the number of returned values and their
1730   // types. Assume all registers available to the return convention
1731   // are used.
1732   fields[TypeFunc::Parms] = TypePtr::BOTTOM;
1733   uint i = 1;
1734   for (; i < SharedRuntime::java_return_convention_max_int; i++) {
1735     fields[TypeFunc::Parms+i] = TypeInt::INT;
1736   }
1737   for (; i < total; i+=2) {
1738     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1739     fields[TypeFunc::Parms+i+1] = Type::HALF;
1740   }
1741   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1742 
1743   // create result type (range)
1744   fields = TypeTuple::fields(1);
1745   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1746 
1747   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1748 
1749   return TypeFunc::make(domain, range);
1750 }
1751 
1752 const TypeFunc *OptoRuntime::pack_inline_type_Type() {
1753   // create input type (domain)
1754   uint total = 1 + SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1755   const Type **fields = TypeTuple::fields(total);
1756   // We don't know the number of returned values and their
1757   // types. Assume all registers available to the return convention
1758   // are used.
1759   fields[TypeFunc::Parms] = TypeRawPtr::BOTTOM;
1760   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;
1761   uint i = 2;
1762   for (; i < SharedRuntime::java_return_convention_max_int+1; i++) {
1763     fields[TypeFunc::Parms+i] = TypeInt::INT;
1764   }
1765   for (; i < total; i+=2) {
1766     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1767     fields[TypeFunc::Parms+i+1] = Type::HALF;
1768   }
1769   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1770 
1771   // create result type (range)
1772   fields = TypeTuple::fields(1);
1773   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1774 
1775   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1776 
1777   return TypeFunc::make(domain, range);
1778 }
1779 
1780 JRT_BLOCK_ENTRY(void, OptoRuntime::load_unknown_inline(flatArrayOopDesc* array, int index, JavaThread* current))
1781   JRT_BLOCK;
1782   flatArrayHandle vah(current, array);
1783   oop buffer = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, THREAD);
1784   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
1785   current->set_vm_result(buffer);
1786   JRT_BLOCK_END;
1787 JRT_END
1788 
1789 const TypeFunc* OptoRuntime::load_unknown_inline_type() {
1790   // create input type (domain)
1791   const Type** fields = TypeTuple::fields(2);
1792   fields[TypeFunc::Parms] = TypeOopPtr::NOTNULL;
1793   fields[TypeFunc::Parms+1] = TypeInt::POS;
1794 
1795   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+2, fields);
1796 
1797   // create result type (range)
1798   fields = TypeTuple::fields(1);
1799   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1800 
1801   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1802 
1803   return TypeFunc::make(domain, range);
1804 }
1805 
1806 JRT_LEAF(void, OptoRuntime::store_unknown_inline(instanceOopDesc* buffer, flatArrayOopDesc* array, int index))
1807 {
1808   assert(buffer != NULL, "can't store null into flat array");
1809   array->value_copy_to_index(buffer, index);
1810 }
1811 JRT_END
1812 
1813 const TypeFunc* OptoRuntime::store_unknown_inline_type() {
1814   // create input type (domain)
1815   const Type** fields = TypeTuple::fields(3);
1816   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1817   fields[TypeFunc::Parms+1] = TypeOopPtr::NOTNULL;
1818   fields[TypeFunc::Parms+2] = TypeInt::POS;
1819 
1820   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1821 
1822   // create result type (range)
1823   fields = TypeTuple::fields(0);
1824   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1825 
1826   return TypeFunc::make(domain, range);
1827 }