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; // digest_length
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; // digest_length
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 // Base64 encode function
1233 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1234   int argcnt = 6;
1235 
1236   const Type** fields = TypeTuple::fields(argcnt);
1237   int argp = TypeFunc::Parms;
1238   fields[argp++] = TypePtr::NOTNULL;    // src array
1239   fields[argp++] = TypeInt::INT;        // offset
1240   fields[argp++] = TypeInt::INT;        // length
1241   fields[argp++] = TypePtr::NOTNULL;    // dest array
1242   fields[argp++] = TypeInt::INT;       // dp
1243   fields[argp++] = TypeInt::BOOL;       // isURL
1244   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1245   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1246 
1247   // result type needed
1248   fields = TypeTuple::fields(1);
1249   fields[TypeFunc::Parms + 0] = NULL; // void
1250   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1251   return TypeFunc::make(domain, range);
1252 }
1253 // Base64 decode function
1254 const TypeFunc* OptoRuntime::base64_decodeBlock_Type() {
1255   int argcnt = 7;
1256 
1257   const Type** fields = TypeTuple::fields(argcnt);
1258   int argp = TypeFunc::Parms;
1259   fields[argp++] = TypePtr::NOTNULL;    // src array
1260   fields[argp++] = TypeInt::INT;        // src offset
1261   fields[argp++] = TypeInt::INT;        // src length
1262   fields[argp++] = TypePtr::NOTNULL;    // dest array
1263   fields[argp++] = TypeInt::INT;        // dest offset
1264   fields[argp++] = TypeInt::BOOL;       // isURL
1265   fields[argp++] = TypeInt::BOOL;       // isMIME
1266   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1267   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1268 
1269   // result type needed
1270   fields = TypeTuple::fields(1);
1271   fields[TypeFunc::Parms + 0] = TypeInt::INT; // count of bytes written to dst
1272   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1273   return TypeFunc::make(domain, range);
1274 }
1275 
1276 //------------- Interpreter state access for on stack replacement
1277 const TypeFunc* OptoRuntime::osr_end_Type() {
1278   // create input type (domain)
1279   const Type **fields = TypeTuple::fields(1);
1280   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1281   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1282 
1283   // create result type
1284   fields = TypeTuple::fields(1);
1285   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1286   fields[TypeFunc::Parms+0] = NULL; // void
1287   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1288   return TypeFunc::make(domain, range);
1289 }
1290 
1291 //-------------------------------------------------------------------------------------
1292 // register policy
1293 
1294 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1295   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1296   switch (register_save_policy[reg]) {
1297     case 'C': return false; //SOC
1298     case 'E': return true ; //SOE
1299     case 'N': return false; //NS
1300     case 'A': return false; //AS
1301   }
1302   ShouldNotReachHere();
1303   return false;
1304 }
1305 
1306 //-----------------------------------------------------------------------
1307 // Exceptions
1308 //
1309 
1310 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1311 
1312 // The method is an entry that is always called by a C++ method not
1313 // directly from compiled code. Compiled code will call the C++ method following.
1314 // We can't allow async exception to be installed during  exception processing.
1315 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1316   // The frame we rethrow the exception to might not have been processed by the GC yet.
1317   // The stack watermark barrier takes care of detecting that and ensuring the frame
1318   // has updated oops.
1319   StackWatermarkSet::after_unwind(current);
1320 
1321   // Do not confuse exception_oop with pending_exception. The exception_oop
1322   // is only used to pass arguments into the method. Not for general
1323   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1324   // the runtime stubs checks this on exit.
1325   assert(current->exception_oop() != NULL, "exception oop is found");
1326   address handler_address = NULL;
1327 
1328   Handle exception(current, current->exception_oop());
1329   address pc = current->exception_pc();
1330 
1331   // Clear out the exception oop and pc since looking up an
1332   // exception handler can cause class loading, which might throw an
1333   // exception and those fields are expected to be clear during
1334   // normal bytecode execution.
1335   current->clear_exception_oop_and_pc();
1336 
1337   LogTarget(Info, exceptions) lt;
1338   if (lt.is_enabled()) {
1339     ResourceMark rm;
1340     LogStream ls(lt);
1341     trace_exception(&ls, exception(), pc, "");
1342   }
1343 
1344   // for AbortVMOnException flag
1345   Exceptions::debug_check_abort(exception);
1346 
1347 #ifdef ASSERT
1348   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1349     // should throw an exception here
1350     ShouldNotReachHere();
1351   }
1352 #endif
1353 
1354   // new exception handling: this method is entered only from adapters
1355   // exceptions from compiled java methods are handled in compiled code
1356   // using rethrow node
1357 
1358   nm = CodeCache::find_nmethod(pc);
1359   assert(nm != NULL, "No NMethod found");
1360   if (nm->is_native_method()) {
1361     fatal("Native method should not have path to exception handling");
1362   } else {
1363     // we are switching to old paradigm: search for exception handler in caller_frame
1364     // instead in exception handler of caller_frame.sender()
1365 
1366     if (JvmtiExport::can_post_on_exceptions()) {
1367       // "Full-speed catching" is not necessary here,
1368       // since we're notifying the VM on every catch.
1369       // Force deoptimization and the rest of the lookup
1370       // will be fine.
1371       deoptimize_caller_frame(current);
1372     }
1373 
1374     // Check the stack guard pages.  If enabled, look for handler in this frame;
1375     // otherwise, forcibly unwind the frame.
1376     //
1377     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1378     bool force_unwind = !current->stack_overflow_state()->reguard_stack();
1379     bool deopting = false;
1380     if (nm->is_deopt_pc(pc)) {
1381       deopting = true;
1382       RegisterMap map(current,
1383                       RegisterMap::UpdateMap::skip,
1384                       RegisterMap::ProcessFrames::include,
1385                       RegisterMap::WalkContinuation::skip);
1386       frame deoptee = current->last_frame().sender(&map);
1387       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1388       // Adjust the pc back to the original throwing pc
1389       pc = deoptee.pc();
1390     }
1391 
1392     // If we are forcing an unwind because of stack overflow then deopt is
1393     // irrelevant since we are throwing the frame away anyway.
1394 
1395     if (deopting && !force_unwind) {
1396       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1397     } else {
1398 
1399       handler_address =
1400         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
1401 
1402       if (handler_address == NULL) {
1403         bool recursive_exception = false;
1404         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1405         assert (handler_address != NULL, "must have compiled handler");
1406         // Update the exception cache only when the unwind was not forced
1407         // and there didn't happen another exception during the computation of the
1408         // compiled exception handler. Checking for exception oop equality is not
1409         // sufficient because some exceptions are pre-allocated and reused.
1410         if (!force_unwind && !recursive_exception) {
1411           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1412         }
1413       } else {
1414 #ifdef ASSERT
1415         bool recursive_exception = false;
1416         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1417         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1418                  p2i(handler_address), p2i(computed_address));
1419 #endif
1420       }
1421     }
1422 
1423     current->set_exception_pc(pc);
1424     current->set_exception_handler_pc(handler_address);
1425 
1426     // Check if the exception PC is a MethodHandle call site.
1427     current->set_is_method_handle_return(nm->is_method_handle_return(pc));
1428   }
1429 
1430   // Restore correct return pc.  Was saved above.
1431   current->set_exception_oop(exception());
1432   return handler_address;
1433 
1434 JRT_END
1435 
1436 // We are entering here from exception_blob
1437 // If there is a compiled exception handler in this method, we will continue there;
1438 // otherwise we will unwind the stack and continue at the caller of top frame method
1439 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1440 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1441 // we looked up the handler for has been deoptimized in the meantime. If it has been
1442 // we must not use the handler and instead return the deopt blob.
1443 address OptoRuntime::handle_exception_C(JavaThread* current) {
1444 //
1445 // We are in Java not VM and in debug mode we have a NoHandleMark
1446 //
1447 #ifndef PRODUCT
1448   SharedRuntime::_find_handler_ctr++;          // find exception handler
1449 #endif
1450   debug_only(NoHandleMark __hm;)
1451   nmethod* nm = NULL;
1452   address handler_address = NULL;
1453   {
1454     // Enter the VM
1455 
1456     ResetNoHandleMark rnhm;
1457     handler_address = handle_exception_C_helper(current, nm);
1458   }
1459 
1460   // Back in java: Use no oops, DON'T safepoint
1461 
1462   // Now check to see if the handler we are returning is in a now
1463   // deoptimized frame
1464 
1465   if (nm != NULL) {
1466     RegisterMap map(current,
1467                     RegisterMap::UpdateMap::skip,
1468                     RegisterMap::ProcessFrames::skip,
1469                     RegisterMap::WalkContinuation::skip);
1470     frame caller = current->last_frame().sender(&map);
1471 #ifdef ASSERT
1472     assert(caller.is_compiled_frame(), "must be");
1473 #endif // ASSERT
1474     if (caller.is_deoptimized_frame()) {
1475       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1476     }
1477   }
1478   return handler_address;
1479 }
1480 
1481 //------------------------------rethrow----------------------------------------
1482 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1483 // is either throwing or rethrowing an exception.  The callee-save registers
1484 // have been restored, synchronized objects have been unlocked and the callee
1485 // stack frame has been removed.  The return address was passed in.
1486 // Exception oop is passed as the 1st argument.  This routine is then called
1487 // from the stub.  On exit, we know where to jump in the caller's code.
1488 // After this C code exits, the stub will pop his frame and end in a jump
1489 // (instead of a return).  We enter the caller's default handler.
1490 //
1491 // This must be JRT_LEAF:
1492 //     - caller will not change its state as we cannot block on exit,
1493 //       therefore raw_exception_handler_for_return_address is all it takes
1494 //       to handle deoptimized blobs
1495 //
1496 // However, there needs to be a safepoint check in the middle!  So compiled
1497 // safepoints are completely watertight.
1498 //
1499 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1500 //
1501 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1502 //
1503 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1504 
1505   // Enable WXWrite: the function called directly by compiled code.
1506   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
1507 
1508   // ret_pc will have been loaded from the stack, so for AArch64 will be signed.
1509   // This needs authenticating, but to do that here requires the fp of the previous frame.
1510   // A better way of doing it would be authenticate in the caller by adding a
1511   // AuthPAuthNode and using it in GraphKit::gen_stub. For now, just strip it.
1512   AARCH64_PORT_ONLY(ret_pc = pauth_strip_pointer(ret_pc));
1513 
1514 #ifndef PRODUCT
1515   SharedRuntime::_rethrow_ctr++;               // count rethrows
1516 #endif
1517   assert (exception != NULL, "should have thrown a NULLPointerException");
1518 #ifdef ASSERT
1519   if (!(exception->is_a(vmClasses::Throwable_klass()))) {
1520     // should throw an exception here
1521     ShouldNotReachHere();
1522   }
1523 #endif
1524 
1525   thread->set_vm_result(exception);
1526   // Frame not compiled (handles deoptimization blob)
1527   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1528 }
1529 
1530 
1531 const TypeFunc *OptoRuntime::rethrow_Type() {
1532   // create input type (domain)
1533   const Type **fields = TypeTuple::fields(1);
1534   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1535   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1536 
1537   // create result type (range)
1538   fields = TypeTuple::fields(1);
1539   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1540   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1541 
1542   return TypeFunc::make(domain, range);
1543 }
1544 
1545 
1546 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1547   // Deoptimize the caller before continuing, as the compiled
1548   // exception handler table may not be valid.
1549   if (!StressCompiledExceptionHandlers && doit) {
1550     deoptimize_caller_frame(thread);
1551   }
1552 }
1553 
1554 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1555   // Called from within the owner thread, so no need for safepoint
1556   RegisterMap reg_map(thread,
1557                       RegisterMap::UpdateMap::include,
1558                       RegisterMap::ProcessFrames::include,
1559                       RegisterMap::WalkContinuation::skip);
1560   frame stub_frame = thread->last_frame();
1561   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1562   frame caller_frame = stub_frame.sender(&reg_map);
1563 
1564   // Deoptimize the caller frame.
1565   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1566 }
1567 
1568 
1569 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1570   // Called from within the owner thread, so no need for safepoint
1571   RegisterMap reg_map(thread,
1572                       RegisterMap::UpdateMap::include,
1573                       RegisterMap::ProcessFrames::include,
1574                       RegisterMap::WalkContinuation::skip);
1575   frame stub_frame = thread->last_frame();
1576   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1577   frame caller_frame = stub_frame.sender(&reg_map);
1578   return caller_frame.is_deoptimized_frame();
1579 }
1580 
1581 
1582 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1583   // create input type (domain)
1584   const Type **fields = TypeTuple::fields(1);
1585   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1586   // // The JavaThread* is passed to each routine as the last argument
1587   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1588   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1589 
1590   // create result type (range)
1591   fields = TypeTuple::fields(0);
1592 
1593   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1594 
1595   return TypeFunc::make(domain, range);
1596 }
1597 
1598 #if INCLUDE_JFR
1599 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1600   // create input type (domain)
1601   const Type **fields = TypeTuple::fields(1);
1602   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1603   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1604 
1605   // create result type (range)
1606   fields = TypeTuple::fields(0);
1607 
1608   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1609 
1610   return TypeFunc::make(domain,range);
1611 }
1612 #endif
1613 
1614 //-----------------------------------------------------------------------------
1615 // Dtrace support.  entry and exit probes have the same signature
1616 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1617   // create input type (domain)
1618   const Type **fields = TypeTuple::fields(2);
1619   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1620   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1621   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1622 
1623   // create result type (range)
1624   fields = TypeTuple::fields(0);
1625 
1626   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1627 
1628   return TypeFunc::make(domain, range);
1629 }
1630 
1631 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1632   // create input type (domain)
1633   const Type **fields = TypeTuple::fields(2);
1634   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1635   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1636 
1637   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1638 
1639   // create result type (range)
1640   fields = TypeTuple::fields(0);
1641 
1642   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1643 
1644   return TypeFunc::make(domain, range);
1645 }
1646 
1647 
1648 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1649   assert(oopDesc::is_oop(obj), "must be a valid oop");
1650   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1651   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1652 JRT_END
1653 
1654 //-----------------------------------------------------------------------------
1655 
1656 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1657 
1658 //
1659 // dump the collected NamedCounters.
1660 //
1661 void OptoRuntime::print_named_counters() {
1662   int total_lock_count = 0;
1663   int eliminated_lock_count = 0;
1664 
1665   NamedCounter* c = _named_counters;
1666   while (c) {
1667     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1668       int count = c->count();
1669       if (count > 0) {
1670         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1671         if (Verbose) {
1672           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1673         }
1674         total_lock_count += count;
1675         if (eliminated) {
1676           eliminated_lock_count += count;
1677         }
1678       }
1679 #if INCLUDE_RTM_OPT
1680     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1681       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1682       if (rlc->nonzero()) {
1683         tty->print_cr("%s", c->name());
1684         rlc->print_on(tty);
1685       }
1686 #endif
1687     }
1688     c = c->next();
1689   }
1690   if (total_lock_count > 0) {
1691     tty->print_cr("dynamic locks: %d", total_lock_count);
1692     if (eliminated_lock_count) {
1693       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1694                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1695     }
1696   }
1697 }
1698 
1699 //
1700 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1701 //  name which consists of method@line for the inlining tree.
1702 //
1703 
1704 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1705   int max_depth = youngest_jvms->depth();
1706 
1707   // Visit scopes from youngest to oldest.
1708   bool first = true;
1709   stringStream st;
1710   for (int depth = max_depth; depth >= 1; depth--) {
1711     JVMState* jvms = youngest_jvms->of_depth(depth);
1712     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1713     if (!first) {
1714       st.print(" ");
1715     } else {
1716       first = false;
1717     }
1718     int bci = jvms->bci();
1719     if (bci < 0) bci = 0;
1720     if (m != NULL) {
1721       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1722     } else {
1723       st.print("no method");
1724     }
1725     st.print("@%d", bci);
1726     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1727   }
1728   NamedCounter* c;
1729   if (tag == NamedCounter::RTMLockingCounter) {
1730     c = new RTMLockingNamedCounter(st.freeze());
1731   } else {
1732     c = new NamedCounter(st.freeze(), tag);
1733   }
1734 
1735   // atomically add the new counter to the head of the list.  We only
1736   // add counters so this is safe.
1737   NamedCounter* head;
1738   do {
1739     c->set_next(NULL);
1740     head = _named_counters;
1741     c->set_next(head);
1742   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1743   return c;
1744 }
1745 
1746 int trace_exception_counter = 0;
1747 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1748   trace_exception_counter++;
1749   stringStream tempst;
1750 
1751   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1752   exception_oop->print_value_on(&tempst);
1753   tempst.print(" in ");
1754   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1755   if (blob->is_compiled()) {
1756     CompiledMethod* cm = blob->as_compiled_method_or_null();
1757     cm->method()->print_value_on(&tempst);
1758   } else if (blob->is_runtime_stub()) {
1759     tempst.print("<runtime-stub>");
1760   } else {
1761     tempst.print("<unknown>");
1762   }
1763   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1764   tempst.print("]");
1765 
1766   st->print_raw_cr(tempst.freeze());
1767 }
1768 
1769 const TypeFunc *OptoRuntime::store_inline_type_fields_Type() {
1770   // create input type (domain)
1771   uint total = SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1772   const Type **fields = TypeTuple::fields(total);
1773   // We don't know the number of returned values and their
1774   // types. Assume all registers available to the return convention
1775   // are used.
1776   fields[TypeFunc::Parms] = TypePtr::BOTTOM;
1777   uint i = 1;
1778   for (; i < SharedRuntime::java_return_convention_max_int; i++) {
1779     fields[TypeFunc::Parms+i] = TypeInt::INT;
1780   }
1781   for (; i < total; i+=2) {
1782     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1783     fields[TypeFunc::Parms+i+1] = Type::HALF;
1784   }
1785   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1786 
1787   // create result type (range)
1788   fields = TypeTuple::fields(1);
1789   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1790 
1791   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1792 
1793   return TypeFunc::make(domain, range);
1794 }
1795 
1796 const TypeFunc *OptoRuntime::pack_inline_type_Type() {
1797   // create input type (domain)
1798   uint total = 1 + SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1799   const Type **fields = TypeTuple::fields(total);
1800   // We don't know the number of returned values and their
1801   // types. Assume all registers available to the return convention
1802   // are used.
1803   fields[TypeFunc::Parms] = TypeRawPtr::BOTTOM;
1804   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;
1805   uint i = 2;
1806   for (; i < SharedRuntime::java_return_convention_max_int+1; i++) {
1807     fields[TypeFunc::Parms+i] = TypeInt::INT;
1808   }
1809   for (; i < total; i+=2) {
1810     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1811     fields[TypeFunc::Parms+i+1] = Type::HALF;
1812   }
1813   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1814 
1815   // create result type (range)
1816   fields = TypeTuple::fields(1);
1817   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1818 
1819   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1820 
1821   return TypeFunc::make(domain, range);
1822 }
1823 
1824 JRT_BLOCK_ENTRY(void, OptoRuntime::load_unknown_inline(flatArrayOopDesc* array, int index, JavaThread* current))
1825   JRT_BLOCK;
1826   flatArrayHandle vah(current, array);
1827   oop buffer = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, THREAD);
1828   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
1829   current->set_vm_result(buffer);
1830   JRT_BLOCK_END;
1831 JRT_END
1832 
1833 const TypeFunc* OptoRuntime::load_unknown_inline_type() {
1834   // create input type (domain)
1835   const Type** fields = TypeTuple::fields(2);
1836   fields[TypeFunc::Parms] = TypeOopPtr::NOTNULL;
1837   fields[TypeFunc::Parms+1] = TypeInt::POS;
1838 
1839   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+2, fields);
1840 
1841   // create result type (range)
1842   fields = TypeTuple::fields(1);
1843   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1844 
1845   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1846 
1847   return TypeFunc::make(domain, range);
1848 }
1849 
1850 JRT_LEAF(void, OptoRuntime::store_unknown_inline(instanceOopDesc* buffer, flatArrayOopDesc* array, int index))
1851 {
1852   assert(buffer != NULL, "can't store null into flat array");
1853   array->value_copy_to_index(buffer, index);
1854 }
1855 JRT_END
1856 
1857 const TypeFunc* OptoRuntime::store_unknown_inline_type() {
1858   // create input type (domain)
1859   const Type** fields = TypeTuple::fields(3);
1860   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1861   fields[TypeFunc::Parms+1] = TypeOopPtr::NOTNULL;
1862   fields[TypeFunc::Parms+2] = TypeInt::POS;
1863 
1864   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1865 
1866   // create result type (range)
1867   fields = TypeTuple::fields(0);
1868   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1869 
1870   return TypeFunc::make(domain, range);
1871 }