1 /*
   2  * Copyright (c) 1998, 2019, 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 "oops/valueArrayKlass.hpp"
  53 #include "oops/valueArrayOop.inline.hpp"
  54 #include "opto/ad.hpp"
  55 #include "opto/addnode.hpp"
  56 #include "opto/callnode.hpp"
  57 #include "opto/cfgnode.hpp"
  58 #include "opto/graphKit.hpp"
  59 #include "opto/machnode.hpp"
  60 #include "opto/matcher.hpp"
  61 #include "opto/memnode.hpp"
  62 #include "opto/mulnode.hpp"
  63 #include "opto/runtime.hpp"
  64 #include "opto/subnode.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/interfaceSupport.inline.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/sharedRuntime.hpp"
  71 #include "runtime/signature.hpp"
  72 #include "runtime/threadCritical.hpp"
  73 #include "runtime/vframe.hpp"
  74 #include "runtime/vframeArray.hpp"
  75 #include "runtime/vframe_hp.hpp"
  76 #include "utilities/copy.hpp"
  77 #include "utilities/preserveException.hpp"
  78 
  79 
  80 // For debugging purposes:
  81 //  To force FullGCALot inside a runtime function, add the following two lines
  82 //
  83 //  Universe::release_fullgc_alot_dummy();
  84 //  MarkSweep::invoke(0, "Debugging");
  85 //
  86 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  87 
  88 
  89 
  90 
  91 // Compiled code entry points
  92 address OptoRuntime::_new_instance_Java                           = NULL;
  93 address OptoRuntime::_new_array_Java                              = NULL;
  94 address OptoRuntime::_new_array_nozero_Java                       = NULL;
  95 address OptoRuntime::_multianewarray2_Java                        = NULL;
  96 address OptoRuntime::_multianewarray3_Java                        = NULL;
  97 address OptoRuntime::_multianewarray4_Java                        = NULL;
  98 address OptoRuntime::_multianewarray5_Java                        = NULL;
  99 address OptoRuntime::_multianewarrayN_Java                        = NULL;
 100 address OptoRuntime::_vtable_must_compile_Java                    = NULL;
 101 address OptoRuntime::_complete_monitor_locking_Java               = NULL;
 102 address OptoRuntime::_monitor_notify_Java                         = NULL;
 103 address OptoRuntime::_monitor_notifyAll_Java                      = NULL;
 104 address OptoRuntime::_rethrow_Java                                = NULL;
 105 
 106 address OptoRuntime::_slow_arraycopy_Java                         = NULL;
 107 address OptoRuntime::_register_finalizer_Java                     = NULL;
 108 
 109 ExceptionBlob* OptoRuntime::_exception_blob;
 110 
 111 // This should be called in an assertion at the start of OptoRuntime routines
 112 // which are entered from compiled code (all of them)
 113 #ifdef ASSERT
 114 static bool check_compiled_frame(JavaThread* thread) {
 115   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 116   RegisterMap map(thread, false);
 117   frame caller = thread->last_frame().sender(&map);
 118   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 119   return true;
 120 }
 121 #endif // ASSERT
 122 
 123 
 124 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, save_arg_regs, return_pc) \
 125   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, save_arg_regs, return_pc); \
 126   if (var == NULL) { return false; }
 127 
 128 bool OptoRuntime::generate(ciEnv* env) {
 129 
 130   generate_exception_blob();
 131 
 132   // Note: tls: Means fetching the return oop out of the thread-local storage
 133   //
 134   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,save_args,retpc
 135   // -------------------------------------------------------------------------------------------------------------------------------
 136   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true , false, false);
 137   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true , false, false);
 138   gen(env, _new_array_nozero_Java          , new_array_Type               , new_array_nozero_C              ,    0 , true , false, false);
 139   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true , false, false);
 140   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true , false, false);
 141   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true , false, false);
 142   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true , false, false);
 143   gen(env, _multianewarrayN_Java           , multianewarrayN_Type         , multianewarrayN_C               ,    0 , true , false, false);
 144   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C, 0, false, false, false);
 145   gen(env, _monitor_notify_Java            , monitor_notify_Type          , monitor_notify_C                ,    0 , false, false, false);
 146   gen(env, _monitor_notifyAll_Java         , monitor_notify_Type          , monitor_notifyAll_C             ,    0 , false, false, false);
 147   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , false, true );
 148 
 149   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false, false);
 150   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false, false);
 151 
 152   return true;
 153 }
 154 
 155 #undef gen
 156 
 157 
 158 // Helper method to do generation of RunTimeStub's
 159 address OptoRuntime::generate_stub( ciEnv* env,
 160                                     TypeFunc_generator gen, address C_function,
 161                                     const char *name, int is_fancy_jump,
 162                                     bool pass_tls,
 163                                     bool save_argument_registers,
 164                                     bool return_pc) {
 165 
 166   // Matching the default directive, we currently have no method to match.
 167   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
 168   ResourceMark rm;
 169   Compile C( env, gen, C_function, name, is_fancy_jump, pass_tls, save_argument_registers, return_pc, directive);
 170   DirectivesStack::release(directive);
 171   return  C.stub_entry_point();
 172 }
 173 
 174 const char* OptoRuntime::stub_name(address entry) {
 175 #ifndef PRODUCT
 176   CodeBlob* cb = CodeCache::find_blob(entry);
 177   RuntimeStub* rs =(RuntimeStub *)cb;
 178   assert(rs != NULL && rs->is_runtime_stub(), "not a runtime stub");
 179   return rs->name();
 180 #else
 181   // Fast implementation for product mode (maybe it should be inlined too)
 182   return "runtime stub";
 183 #endif
 184 }
 185 
 186 
 187 //=============================================================================
 188 // Opto compiler runtime routines
 189 //=============================================================================
 190 
 191 
 192 //=============================allocation======================================
 193 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 194 // and try allocation again.
 195 
 196 // object allocation
 197 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* thread))
 198   JRT_BLOCK;
 199 #ifndef PRODUCT
 200   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 201 #endif
 202   assert(check_compiled_frame(thread), "incorrect caller");
 203 
 204   // These checks are cheap to make and support reflective allocation.
 205   int lh = klass->layout_helper();
 206   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 207     Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 208     klass->check_valid_for_instantiation(false, THREAD);
 209     if (!HAS_PENDING_EXCEPTION) {
 210       InstanceKlass::cast(klass)->initialize(THREAD);
 211     }
 212   }
 213 
 214   if (!HAS_PENDING_EXCEPTION) {
 215     // Scavenge and allocate an instance.
 216     Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
 217     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 218     thread->set_vm_result(result);
 219 
 220     // Pass oops back through thread local storage.  Our apparent type to Java
 221     // is that we return an oop, but we can block on exit from this routine and
 222     // a GC can trash the oop in C's return register.  The generated stub will
 223     // fetch the oop from TLS after any possible GC.
 224   }
 225 
 226   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 227   JRT_BLOCK_END;
 228 
 229   // inform GC that we won't do card marks for initializing writes.
 230   SharedRuntime::on_slowpath_allocation_exit(thread);
 231 JRT_END
 232 
 233 
 234 // array allocation
 235 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread *thread))
 236   JRT_BLOCK;
 237 #ifndef PRODUCT
 238   SharedRuntime::_new_array_ctr++;            // new array requires GC
 239 #endif
 240   assert(check_compiled_frame(thread), "incorrect caller");
 241 
 242   // Scavenge and allocate an instance.
 243   oop result;
 244 
 245   if (array_type->is_valueArray_klass()) {
 246     Klass* elem_type = ValueArrayKlass::cast(array_type)->element_klass();
 247     result = oopFactory::new_valueArray(elem_type, len, THREAD);
 248   } else if (array_type->is_typeArray_klass()) {
 249     // The oopFactory likes to work with the element type.
 250     // (We could bypass the oopFactory, since it doesn't add much value.)
 251     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 252     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 253   } else {
 254     Handle holder(THREAD, array_type->klass_holder()); // keep the array klass alive
 255     result = ObjArrayKlass::cast(array_type)->allocate(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 = (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::sha_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::vectorizedMismatch_Type() {
1116   // create input type (domain)
1117   int num_args = 4;
1118   int argcnt = num_args;
1119   const Type** fields = TypeTuple::fields(argcnt);
1120   int argp = TypeFunc::Parms;
1121   fields[argp++] = TypePtr::NOTNULL;    // obja
1122   fields[argp++] = TypePtr::NOTNULL;    // objb
1123   fields[argp++] = TypeInt::INT;        // length, number of elements
1124   fields[argp++] = TypeInt::INT;        // log2scale, element size
1125   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1126   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1127 
1128   //return mismatch index (int)
1129   fields = TypeTuple::fields(1);
1130   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1131   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1132   return TypeFunc::make(domain, range);
1133 }
1134 
1135 // GHASH block processing
1136 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1137     int argcnt = 4;
1138 
1139     const Type** fields = TypeTuple::fields(argcnt);
1140     int argp = TypeFunc::Parms;
1141     fields[argp++] = TypePtr::NOTNULL;    // state
1142     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1143     fields[argp++] = TypePtr::NOTNULL;    // data
1144     fields[argp++] = TypeInt::INT;        // blocks
1145     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1146     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1147 
1148     // result type needed
1149     fields = TypeTuple::fields(1);
1150     fields[TypeFunc::Parms+0] = NULL; // void
1151     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1152     return TypeFunc::make(domain, range);
1153 }
1154 // Base64 encode function
1155 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1156   int argcnt = 6;
1157 
1158   const Type** fields = TypeTuple::fields(argcnt);
1159   int argp = TypeFunc::Parms;
1160   fields[argp++] = TypePtr::NOTNULL;    // src array
1161   fields[argp++] = TypeInt::INT;        // offset
1162   fields[argp++] = TypeInt::INT;        // length
1163   fields[argp++] = TypePtr::NOTNULL;    // dest array
1164   fields[argp++] = TypeInt::INT;       // dp
1165   fields[argp++] = TypeInt::BOOL;       // isURL
1166   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1167   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1168 
1169   // result type needed
1170   fields = TypeTuple::fields(1);
1171   fields[TypeFunc::Parms + 0] = NULL; // void
1172   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1173   return TypeFunc::make(domain, range);
1174 }
1175 
1176 //------------- Interpreter state access for on stack replacement
1177 const TypeFunc* OptoRuntime::osr_end_Type() {
1178   // create input type (domain)
1179   const Type **fields = TypeTuple::fields(1);
1180   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1181   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1182 
1183   // create result type
1184   fields = TypeTuple::fields(1);
1185   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1186   fields[TypeFunc::Parms+0] = NULL; // void
1187   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1188   return TypeFunc::make(domain, range);
1189 }
1190 
1191 //-------------- methodData update helpers
1192 
1193 const TypeFunc* OptoRuntime::profile_receiver_type_Type() {
1194   // create input type (domain)
1195   const Type **fields = TypeTuple::fields(2);
1196   fields[TypeFunc::Parms+0] = TypeAryPtr::NOTNULL;    // methodData pointer
1197   fields[TypeFunc::Parms+1] = TypeInstPtr::BOTTOM;    // receiver oop
1198   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
1199 
1200   // create result type
1201   fields = TypeTuple::fields(1);
1202   fields[TypeFunc::Parms+0] = NULL; // void
1203   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1204   return TypeFunc::make(domain, range);
1205 }
1206 
1207 JRT_LEAF(void, OptoRuntime::profile_receiver_type_C(DataLayout* data, oopDesc* receiver))
1208   if (receiver == NULL) return;
1209   Klass* receiver_klass = receiver->klass();
1210 
1211   intptr_t* mdp = ((intptr_t*)(data)) + DataLayout::header_size_in_cells();
1212   int empty_row = -1;           // free row, if any is encountered
1213 
1214   // ReceiverTypeData* vc = new ReceiverTypeData(mdp);
1215   for (uint row = 0; row < ReceiverTypeData::row_limit(); row++) {
1216     // if (vc->receiver(row) == receiver_klass)
1217     int receiver_off = ReceiverTypeData::receiver_cell_index(row);
1218     intptr_t row_recv = *(mdp + receiver_off);
1219     if (row_recv == (intptr_t) receiver_klass) {
1220       // vc->set_receiver_count(row, vc->receiver_count(row) + DataLayout::counter_increment);
1221       int count_off = ReceiverTypeData::receiver_count_cell_index(row);
1222       *(mdp + count_off) += DataLayout::counter_increment;
1223       return;
1224     } else if (row_recv == 0) {
1225       // else if (vc->receiver(row) == NULL)
1226       empty_row = (int) row;
1227     }
1228   }
1229 
1230   if (empty_row != -1) {
1231     int receiver_off = ReceiverTypeData::receiver_cell_index(empty_row);
1232     // vc->set_receiver(empty_row, receiver_klass);
1233     *(mdp + receiver_off) = (intptr_t) receiver_klass;
1234     // vc->set_receiver_count(empty_row, DataLayout::counter_increment);
1235     int count_off = ReceiverTypeData::receiver_count_cell_index(empty_row);
1236     *(mdp + count_off) = DataLayout::counter_increment;
1237   } else {
1238     // Receiver did not match any saved receiver and there is no empty row for it.
1239     // Increment total counter to indicate polymorphic case.
1240     intptr_t* count_p = (intptr_t*)(((uint8_t*)(data)) + in_bytes(CounterData::count_offset()));
1241     *count_p += DataLayout::counter_increment;
1242   }
1243 JRT_END
1244 
1245 //-------------------------------------------------------------------------------------
1246 // register policy
1247 
1248 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1249   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1250   switch (register_save_policy[reg]) {
1251     case 'C': return false; //SOC
1252     case 'E': return true ; //SOE
1253     case 'N': return false; //NS
1254     case 'A': return false; //AS
1255   }
1256   ShouldNotReachHere();
1257   return false;
1258 }
1259 
1260 //-----------------------------------------------------------------------
1261 // Exceptions
1262 //
1263 
1264 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1265 
1266 // The method is an entry that is always called by a C++ method not
1267 // directly from compiled code. Compiled code will call the C++ method following.
1268 // We can't allow async exception to be installed during  exception processing.
1269 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* thread, nmethod* &nm))
1270 
1271   // Do not confuse exception_oop with pending_exception. The exception_oop
1272   // is only used to pass arguments into the method. Not for general
1273   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1274   // the runtime stubs checks this on exit.
1275   assert(thread->exception_oop() != NULL, "exception oop is found");
1276   address handler_address = NULL;
1277 
1278   Handle exception(thread, thread->exception_oop());
1279   address pc = thread->exception_pc();
1280 
1281   // Clear out the exception oop and pc since looking up an
1282   // exception handler can cause class loading, which might throw an
1283   // exception and those fields are expected to be clear during
1284   // normal bytecode execution.
1285   thread->clear_exception_oop_and_pc();
1286 
1287   LogTarget(Info, exceptions) lt;
1288   if (lt.is_enabled()) {
1289     ResourceMark rm;
1290     LogStream ls(lt);
1291     trace_exception(&ls, exception(), pc, "");
1292   }
1293 
1294   // for AbortVMOnException flag
1295   Exceptions::debug_check_abort(exception);
1296 
1297 #ifdef ASSERT
1298   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1299     // should throw an exception here
1300     ShouldNotReachHere();
1301   }
1302 #endif
1303 
1304   // new exception handling: this method is entered only from adapters
1305   // exceptions from compiled java methods are handled in compiled code
1306   // using rethrow node
1307 
1308   nm = CodeCache::find_nmethod(pc);
1309   assert(nm != NULL, "No NMethod found");
1310   if (nm->is_native_method()) {
1311     fatal("Native method should not have path to exception handling");
1312   } else {
1313     // we are switching to old paradigm: search for exception handler in caller_frame
1314     // instead in exception handler of caller_frame.sender()
1315 
1316     if (JvmtiExport::can_post_on_exceptions()) {
1317       // "Full-speed catching" is not necessary here,
1318       // since we're notifying the VM on every catch.
1319       // Force deoptimization and the rest of the lookup
1320       // will be fine.
1321       deoptimize_caller_frame(thread);
1322     }
1323 
1324     // Check the stack guard pages.  If enabled, look for handler in this frame;
1325     // otherwise, forcibly unwind the frame.
1326     //
1327     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1328     bool force_unwind = !thread->reguard_stack();
1329     bool deopting = false;
1330     if (nm->is_deopt_pc(pc)) {
1331       deopting = true;
1332       RegisterMap map(thread, false);
1333       frame deoptee = thread->last_frame().sender(&map);
1334       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1335       // Adjust the pc back to the original throwing pc
1336       pc = deoptee.pc();
1337     }
1338 
1339     // If we are forcing an unwind because of stack overflow then deopt is
1340     // irrelevant since we are throwing the frame away anyway.
1341 
1342     if (deopting && !force_unwind) {
1343       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1344     } else {
1345 
1346       handler_address =
1347         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
1348 
1349       if (handler_address == NULL) {
1350         bool recursive_exception = false;
1351         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1352         assert (handler_address != NULL, "must have compiled handler");
1353         // Update the exception cache only when the unwind was not forced
1354         // and there didn't happen another exception during the computation of the
1355         // compiled exception handler. Checking for exception oop equality is not
1356         // sufficient because some exceptions are pre-allocated and reused.
1357         if (!force_unwind && !recursive_exception) {
1358           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1359         }
1360       } else {
1361 #ifdef ASSERT
1362         bool recursive_exception = false;
1363         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1364         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1365                  p2i(handler_address), p2i(computed_address));
1366 #endif
1367       }
1368     }
1369 
1370     thread->set_exception_pc(pc);
1371     thread->set_exception_handler_pc(handler_address);
1372 
1373     // Check if the exception PC is a MethodHandle call site.
1374     thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
1375   }
1376 
1377   // Restore correct return pc.  Was saved above.
1378   thread->set_exception_oop(exception());
1379   return handler_address;
1380 
1381 JRT_END
1382 
1383 // We are entering here from exception_blob
1384 // If there is a compiled exception handler in this method, we will continue there;
1385 // otherwise we will unwind the stack and continue at the caller of top frame method
1386 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1387 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1388 // we looked up the handler for has been deoptimized in the meantime. If it has been
1389 // we must not use the handler and instead return the deopt blob.
1390 address OptoRuntime::handle_exception_C(JavaThread* thread) {
1391 //
1392 // We are in Java not VM and in debug mode we have a NoHandleMark
1393 //
1394 #ifndef PRODUCT
1395   SharedRuntime::_find_handler_ctr++;          // find exception handler
1396 #endif
1397   debug_only(NoHandleMark __hm;)
1398   nmethod* nm = NULL;
1399   address handler_address = NULL;
1400   {
1401     // Enter the VM
1402 
1403     ResetNoHandleMark rnhm;
1404     handler_address = handle_exception_C_helper(thread, nm);
1405   }
1406 
1407   // Back in java: Use no oops, DON'T safepoint
1408 
1409   // Now check to see if the handler we are returning is in a now
1410   // deoptimized frame
1411 
1412   if (nm != NULL) {
1413     RegisterMap map(thread, false);
1414     frame caller = thread->last_frame().sender(&map);
1415 #ifdef ASSERT
1416     assert(caller.is_compiled_frame(), "must be");
1417 #endif // ASSERT
1418     if (caller.is_deoptimized_frame()) {
1419       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1420     }
1421   }
1422   return handler_address;
1423 }
1424 
1425 //------------------------------rethrow----------------------------------------
1426 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1427 // is either throwing or rethrowing an exception.  The callee-save registers
1428 // have been restored, synchronized objects have been unlocked and the callee
1429 // stack frame has been removed.  The return address was passed in.
1430 // Exception oop is passed as the 1st argument.  This routine is then called
1431 // from the stub.  On exit, we know where to jump in the caller's code.
1432 // After this C code exits, the stub will pop his frame and end in a jump
1433 // (instead of a return).  We enter the caller's default handler.
1434 //
1435 // This must be JRT_LEAF:
1436 //     - caller will not change its state as we cannot block on exit,
1437 //       therefore raw_exception_handler_for_return_address is all it takes
1438 //       to handle deoptimized blobs
1439 //
1440 // However, there needs to be a safepoint check in the middle!  So compiled
1441 // safepoints are completely watertight.
1442 //
1443 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1444 //
1445 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1446 //
1447 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1448 #ifndef PRODUCT
1449   SharedRuntime::_rethrow_ctr++;               // count rethrows
1450 #endif
1451   assert (exception != NULL, "should have thrown a NULLPointerException");
1452 #ifdef ASSERT
1453   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1454     // should throw an exception here
1455     ShouldNotReachHere();
1456   }
1457 #endif
1458 
1459   thread->set_vm_result(exception);
1460   // Frame not compiled (handles deoptimization blob)
1461   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1462 }
1463 
1464 
1465 const TypeFunc *OptoRuntime::rethrow_Type() {
1466   // create input type (domain)
1467   const Type **fields = TypeTuple::fields(1);
1468   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1469   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1470 
1471   // create result type (range)
1472   fields = TypeTuple::fields(1);
1473   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1474   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1475 
1476   return TypeFunc::make(domain, range);
1477 }
1478 
1479 
1480 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1481   // Deoptimize the caller before continuing, as the compiled
1482   // exception handler table may not be valid.
1483   if (!StressCompiledExceptionHandlers && doit) {
1484     deoptimize_caller_frame(thread);
1485   }
1486 }
1487 
1488 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1489   // Called from within the owner thread, so no need for safepoint
1490   RegisterMap reg_map(thread);
1491   frame stub_frame = thread->last_frame();
1492   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1493   frame caller_frame = stub_frame.sender(&reg_map);
1494 
1495   // Deoptimize the caller frame.
1496   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1497 }
1498 
1499 
1500 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1501   // Called from within the owner thread, so no need for safepoint
1502   RegisterMap reg_map(thread);
1503   frame stub_frame = thread->last_frame();
1504   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1505   frame caller_frame = stub_frame.sender(&reg_map);
1506   return caller_frame.is_deoptimized_frame();
1507 }
1508 
1509 
1510 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1511   // create input type (domain)
1512   const Type **fields = TypeTuple::fields(1);
1513   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1514   // // The JavaThread* is passed to each routine as the last argument
1515   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1516   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1517 
1518   // create result type (range)
1519   fields = TypeTuple::fields(0);
1520 
1521   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1522 
1523   return TypeFunc::make(domain, range);
1524 }
1525 
1526 
1527 //-----------------------------------------------------------------------------
1528 // Dtrace support.  entry and exit probes have the same signature
1529 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1530   // create input type (domain)
1531   const Type **fields = TypeTuple::fields(2);
1532   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1533   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1534   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1535 
1536   // create result type (range)
1537   fields = TypeTuple::fields(0);
1538 
1539   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1540 
1541   return TypeFunc::make(domain, range);
1542 }
1543 
1544 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1545   // create input type (domain)
1546   const Type **fields = TypeTuple::fields(2);
1547   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1548   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1549 
1550   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1551 
1552   // create result type (range)
1553   fields = TypeTuple::fields(0);
1554 
1555   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1556 
1557   return TypeFunc::make(domain, range);
1558 }
1559 
1560 
1561 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* thread))
1562   assert(oopDesc::is_oop(obj), "must be a valid oop");
1563   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1564   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1565 JRT_END
1566 
1567 //-----------------------------------------------------------------------------
1568 
1569 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1570 
1571 //
1572 // dump the collected NamedCounters.
1573 //
1574 void OptoRuntime::print_named_counters() {
1575   int total_lock_count = 0;
1576   int eliminated_lock_count = 0;
1577 
1578   NamedCounter* c = _named_counters;
1579   while (c) {
1580     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1581       int count = c->count();
1582       if (count > 0) {
1583         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1584         if (Verbose) {
1585           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1586         }
1587         total_lock_count += count;
1588         if (eliminated) {
1589           eliminated_lock_count += count;
1590         }
1591       }
1592     } else if (c->tag() == NamedCounter::BiasedLockingCounter) {
1593       BiasedLockingCounters* blc = ((BiasedLockingNamedCounter*)c)->counters();
1594       if (blc->nonzero()) {
1595         tty->print_cr("%s", c->name());
1596         blc->print_on(tty);
1597       }
1598 #if INCLUDE_RTM_OPT
1599     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1600       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1601       if (rlc->nonzero()) {
1602         tty->print_cr("%s", c->name());
1603         rlc->print_on(tty);
1604       }
1605 #endif
1606     }
1607     c = c->next();
1608   }
1609   if (total_lock_count > 0) {
1610     tty->print_cr("dynamic locks: %d", total_lock_count);
1611     if (eliminated_lock_count) {
1612       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1613                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1614     }
1615   }
1616 }
1617 
1618 //
1619 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1620 //  name which consists of method@line for the inlining tree.
1621 //
1622 
1623 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1624   int max_depth = youngest_jvms->depth();
1625 
1626   // Visit scopes from youngest to oldest.
1627   bool first = true;
1628   stringStream st;
1629   for (int depth = max_depth; depth >= 1; depth--) {
1630     JVMState* jvms = youngest_jvms->of_depth(depth);
1631     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1632     if (!first) {
1633       st.print(" ");
1634     } else {
1635       first = false;
1636     }
1637     int bci = jvms->bci();
1638     if (bci < 0) bci = 0;
1639     if (m != NULL) {
1640       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1641     } else {
1642       st.print("no method");
1643     }
1644     st.print("@%d", bci);
1645     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1646   }
1647   NamedCounter* c;
1648   if (tag == NamedCounter::BiasedLockingCounter) {
1649     c = new BiasedLockingNamedCounter(st.as_string());
1650   } else if (tag == NamedCounter::RTMLockingCounter) {
1651     c = new RTMLockingNamedCounter(st.as_string());
1652   } else {
1653     c = new NamedCounter(st.as_string(), tag);
1654   }
1655 
1656   // atomically add the new counter to the head of the list.  We only
1657   // add counters so this is safe.
1658   NamedCounter* head;
1659   do {
1660     c->set_next(NULL);
1661     head = _named_counters;
1662     c->set_next(head);
1663   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1664   return c;
1665 }
1666 
1667 int trace_exception_counter = 0;
1668 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1669   trace_exception_counter++;
1670   stringStream tempst;
1671 
1672   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1673   exception_oop->print_value_on(&tempst);
1674   tempst.print(" in ");
1675   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1676   if (blob->is_compiled()) {
1677     CompiledMethod* cm = blob->as_compiled_method_or_null();
1678     cm->method()->print_value_on(&tempst);
1679   } else if (blob->is_runtime_stub()) {
1680     tempst.print("<runtime-stub>");
1681   } else {
1682     tempst.print("<unknown>");
1683   }
1684   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1685   tempst.print("]");
1686 
1687   st->print_raw_cr(tempst.as_string());
1688 }
1689 
1690 const TypeFunc *OptoRuntime::store_value_type_fields_Type() {
1691   // create input type (domain)
1692   uint total = SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1693   const Type **fields = TypeTuple::fields(total);
1694   // We don't know the number of returned values and their
1695   // types. Assume all registers available to the return convention
1696   // are used.
1697   fields[TypeFunc::Parms] = TypePtr::BOTTOM;
1698   uint i = 1;
1699   for (; i < SharedRuntime::java_return_convention_max_int; i++) {
1700     fields[TypeFunc::Parms+i] = TypeInt::INT;
1701   }
1702   for (; i < total; i+=2) {
1703     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1704     fields[TypeFunc::Parms+i+1] = Type::HALF;
1705   }
1706   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1707 
1708   // create result type (range)
1709   fields = TypeTuple::fields(1);
1710   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1711 
1712   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1713 
1714   return TypeFunc::make(domain, range);
1715 }
1716 
1717 const TypeFunc *OptoRuntime::pack_value_type_Type() {
1718   // create input type (domain)
1719   uint total = 1 + SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1720   const Type **fields = TypeTuple::fields(total);
1721   // We don't know the number of returned values and their
1722   // types. Assume all registers available to the return convention
1723   // are used.
1724   fields[TypeFunc::Parms] = TypeRawPtr::BOTTOM;
1725   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;
1726   uint i = 2;
1727   for (; i < SharedRuntime::java_return_convention_max_int+1; i++) {
1728     fields[TypeFunc::Parms+i] = TypeInt::INT;
1729   }
1730   for (; i < total; i+=2) {
1731     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1732     fields[TypeFunc::Parms+i+1] = Type::HALF;
1733   }
1734   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1735 
1736   // create result type (range)
1737   fields = TypeTuple::fields(1);
1738   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1739 
1740   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1741 
1742   return TypeFunc::make(domain, range);
1743 }
1744 
1745 JRT_LEAF(void, OptoRuntime::load_unknown_value(valueArrayOopDesc* array, int index, instanceOopDesc* buffer))
1746 {
1747   array->value_copy_from_index(index, buffer);
1748 }
1749 JRT_END
1750 
1751 const TypeFunc *OptoRuntime::load_unknown_value_Type() {
1752   // create input type (domain)
1753   const Type **fields = TypeTuple::fields(3);
1754   // We don't know the number of returned values and their
1755   // types. Assume all registers available to the return convention
1756   // are used.
1757   fields[TypeFunc::Parms] = TypeOopPtr::NOTNULL;
1758   fields[TypeFunc::Parms+1] = TypeInt::POS;
1759   fields[TypeFunc::Parms+2] = TypeInstPtr::NOTNULL;
1760 
1761   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1762 
1763   // create result type (range)
1764   fields = TypeTuple::fields(0);
1765   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1766 
1767   return TypeFunc::make(domain, range);
1768 }
1769 
1770 JRT_LEAF(void, OptoRuntime::store_unknown_value(instanceOopDesc* buffer, valueArrayOopDesc* array, int index))
1771 {
1772   assert(buffer != NULL, "can't store null into flat array");
1773   array->value_copy_to_index(buffer, index);
1774 }
1775 JRT_END
1776 
1777 const TypeFunc *OptoRuntime::store_unknown_value_Type() {
1778   // create input type (domain)
1779   const Type **fields = TypeTuple::fields(3);
1780   // We don't know the number of returned values and their
1781   // types. Assume all registers available to the return convention
1782   // are used.
1783   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1784   fields[TypeFunc::Parms+1] = TypeOopPtr::NOTNULL;
1785   fields[TypeFunc::Parms+2] = TypeInt::POS;
1786 
1787   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1788 
1789   // create result type (range)
1790   fields = TypeTuple::fields(0);
1791   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1792 
1793   return TypeFunc::make(domain, range);
1794 }