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