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