1 /*
   2  * Copyright (c) 1998, 2018, 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 counterMode calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
 905 const TypeFunc* OptoRuntime::counterMode_aescrypt_Type() {
 906   // create input type (domain)
 907   int num_args = 7;
 908   if (Matcher::pass_original_key_for_aes()) {
 909     num_args = 8;
 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++] = TypePtr::NOTNULL; // counter array
 918   fields[argp++] = TypeInt::INT; // src len
 919   fields[argp++] = TypePtr::NOTNULL; // saved_encCounter
 920   fields[argp++] = TypePtr::NOTNULL; // saved used addr
 921   if (Matcher::pass_original_key_for_aes()) {
 922     fields[argp++] = TypePtr::NOTNULL; // original k array
 923   }
 924   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
 925   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
 926   // returning cipher len (int)
 927   fields = TypeTuple::fields(1);
 928   fields[TypeFunc::Parms + 0] = TypeInt::INT;
 929   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
 930   return TypeFunc::make(domain, range);
 931 }
 932 
 933 /*
 934  * void implCompress(byte[] buf, int ofs)
 935  */
 936 const TypeFunc* OptoRuntime::sha_implCompress_Type() {
 937   // create input type (domain)
 938   int num_args = 2;
 939   int argcnt = num_args;
 940   const Type** fields = TypeTuple::fields(argcnt);
 941   int argp = TypeFunc::Parms;
 942   fields[argp++] = TypePtr::NOTNULL; // buf
 943   fields[argp++] = TypePtr::NOTNULL; // state
 944   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 945   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 946 
 947   // no result type needed
 948   fields = TypeTuple::fields(1);
 949   fields[TypeFunc::Parms+0] = NULL; // void
 950   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 951   return TypeFunc::make(domain, range);
 952 }
 953 
 954 /*
 955  * int implCompressMultiBlock(byte[] b, int ofs, int limit)
 956  */
 957 const TypeFunc* OptoRuntime::digestBase_implCompressMB_Type() {
 958   // create input type (domain)
 959   int num_args = 4;
 960   int argcnt = num_args;
 961   const Type** fields = TypeTuple::fields(argcnt);
 962   int argp = TypeFunc::Parms;
 963   fields[argp++] = TypePtr::NOTNULL; // buf
 964   fields[argp++] = TypePtr::NOTNULL; // state
 965   fields[argp++] = TypeInt::INT;     // ofs
 966   fields[argp++] = TypeInt::INT;     // limit
 967   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 968   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 969 
 970   // returning ofs (int)
 971   fields = TypeTuple::fields(1);
 972   fields[TypeFunc::Parms+0] = TypeInt::INT; // ofs
 973   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
 974   return TypeFunc::make(domain, range);
 975 }
 976 
 977 const TypeFunc* OptoRuntime::multiplyToLen_Type() {
 978   // create input type (domain)
 979   int num_args      = 6;
 980   int argcnt = num_args;
 981   const Type** fields = TypeTuple::fields(argcnt);
 982   int argp = TypeFunc::Parms;
 983   fields[argp++] = TypePtr::NOTNULL;    // x
 984   fields[argp++] = TypeInt::INT;        // xlen
 985   fields[argp++] = TypePtr::NOTNULL;    // y
 986   fields[argp++] = TypeInt::INT;        // ylen
 987   fields[argp++] = TypePtr::NOTNULL;    // z
 988   fields[argp++] = TypeInt::INT;        // zlen
 989   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
 990   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 991 
 992   // no result type needed
 993   fields = TypeTuple::fields(1);
 994   fields[TypeFunc::Parms+0] = NULL;
 995   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
 996   return TypeFunc::make(domain, range);
 997 }
 998 
 999 const TypeFunc* OptoRuntime::squareToLen_Type() {
1000   // create input type (domain)
1001   int num_args      = 4;
1002   int argcnt = num_args;
1003   const Type** fields = TypeTuple::fields(argcnt);
1004   int argp = TypeFunc::Parms;
1005   fields[argp++] = TypePtr::NOTNULL;    // x
1006   fields[argp++] = TypeInt::INT;        // len
1007   fields[argp++] = TypePtr::NOTNULL;    // z
1008   fields[argp++] = TypeInt::INT;        // zlen
1009   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1010   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1011 
1012   // no result type needed
1013   fields = TypeTuple::fields(1);
1014   fields[TypeFunc::Parms+0] = NULL;
1015   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1016   return TypeFunc::make(domain, range);
1017 }
1018 
1019 // for mulAdd calls, 2 pointers and 3 ints, returning int
1020 const TypeFunc* OptoRuntime::mulAdd_Type() {
1021   // create input type (domain)
1022   int num_args      = 5;
1023   int argcnt = num_args;
1024   const Type** fields = TypeTuple::fields(argcnt);
1025   int argp = TypeFunc::Parms;
1026   fields[argp++] = TypePtr::NOTNULL;    // out
1027   fields[argp++] = TypePtr::NOTNULL;    // in
1028   fields[argp++] = TypeInt::INT;        // offset
1029   fields[argp++] = TypeInt::INT;        // len
1030   fields[argp++] = TypeInt::INT;        // k
1031   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1032   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1033 
1034   // returning carry (int)
1035   fields = TypeTuple::fields(1);
1036   fields[TypeFunc::Parms+0] = TypeInt::INT;
1037   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1038   return TypeFunc::make(domain, range);
1039 }
1040 
1041 const TypeFunc* OptoRuntime::montgomeryMultiply_Type() {
1042   // create input type (domain)
1043   int num_args      = 7;
1044   int argcnt = num_args;
1045   const Type** fields = TypeTuple::fields(argcnt);
1046   int argp = TypeFunc::Parms;
1047   fields[argp++] = TypePtr::NOTNULL;    // a
1048   fields[argp++] = TypePtr::NOTNULL;    // b
1049   fields[argp++] = TypePtr::NOTNULL;    // n
1050   fields[argp++] = TypeInt::INT;        // len
1051   fields[argp++] = TypeLong::LONG;      // inv
1052   fields[argp++] = Type::HALF;
1053   fields[argp++] = TypePtr::NOTNULL;    // result
1054   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1055   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1056 
1057   // result type needed
1058   fields = TypeTuple::fields(1);
1059   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1060 
1061   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1062   return TypeFunc::make(domain, range);
1063 }
1064 
1065 const TypeFunc* OptoRuntime::montgomerySquare_Type() {
1066   // create input type (domain)
1067   int num_args      = 6;
1068   int argcnt = num_args;
1069   const Type** fields = TypeTuple::fields(argcnt);
1070   int argp = TypeFunc::Parms;
1071   fields[argp++] = TypePtr::NOTNULL;    // a
1072   fields[argp++] = TypePtr::NOTNULL;    // n
1073   fields[argp++] = TypeInt::INT;        // len
1074   fields[argp++] = TypeLong::LONG;      // inv
1075   fields[argp++] = Type::HALF;
1076   fields[argp++] = TypePtr::NOTNULL;    // result
1077   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1078   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1079 
1080   // result type needed
1081   fields = TypeTuple::fields(1);
1082   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1083 
1084   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1085   return TypeFunc::make(domain, range);
1086 }
1087 
1088 const TypeFunc* OptoRuntime::vectorizedMismatch_Type() {
1089   // create input type (domain)
1090   int num_args = 4;
1091   int argcnt = num_args;
1092   const Type** fields = TypeTuple::fields(argcnt);
1093   int argp = TypeFunc::Parms;
1094   fields[argp++] = TypePtr::NOTNULL;    // obja
1095   fields[argp++] = TypePtr::NOTNULL;    // objb
1096   fields[argp++] = TypeInt::INT;        // length, number of elements
1097   fields[argp++] = TypeInt::INT;        // log2scale, element size
1098   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1099   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1100 
1101   //return mismatch index (int)
1102   fields = TypeTuple::fields(1);
1103   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1104   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1105   return TypeFunc::make(domain, range);
1106 }
1107 
1108 // GHASH block processing
1109 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1110     int argcnt = 4;
1111 
1112     const Type** fields = TypeTuple::fields(argcnt);
1113     int argp = TypeFunc::Parms;
1114     fields[argp++] = TypePtr::NOTNULL;    // state
1115     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1116     fields[argp++] = TypePtr::NOTNULL;    // data
1117     fields[argp++] = TypeInt::INT;        // blocks
1118     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1119     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1120 
1121     // result type needed
1122     fields = TypeTuple::fields(1);
1123     fields[TypeFunc::Parms+0] = NULL; // void
1124     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1125     return TypeFunc::make(domain, range);
1126 }
1127 // Base64 encode function
1128 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1129   int argcnt = 6;
1130 
1131   const Type** fields = TypeTuple::fields(argcnt);
1132   int argp = TypeFunc::Parms;
1133   fields[argp++] = TypePtr::NOTNULL;    // src array
1134   fields[argp++] = TypeInt::INT;        // offset
1135   fields[argp++] = TypeInt::INT;        // length
1136   fields[argp++] = TypePtr::NOTNULL;    // dest array
1137   fields[argp++] = TypeInt::INT;       // dp
1138   fields[argp++] = TypeInt::BOOL;       // isURL
1139   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1140   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1141 
1142   // result type needed
1143   fields = TypeTuple::fields(1);
1144   fields[TypeFunc::Parms + 0] = NULL; // void
1145   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1146   return TypeFunc::make(domain, range);
1147 }
1148 
1149 //------------- Interpreter state access for on stack replacement
1150 const TypeFunc* OptoRuntime::osr_end_Type() {
1151   // create input type (domain)
1152   const Type **fields = TypeTuple::fields(1);
1153   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1154   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1155 
1156   // create result type
1157   fields = TypeTuple::fields(1);
1158   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1159   fields[TypeFunc::Parms+0] = NULL; // void
1160   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1161   return TypeFunc::make(domain, range);
1162 }
1163 
1164 //-------------- methodData update helpers
1165 
1166 const TypeFunc* OptoRuntime::profile_receiver_type_Type() {
1167   // create input type (domain)
1168   const Type **fields = TypeTuple::fields(2);
1169   fields[TypeFunc::Parms+0] = TypeAryPtr::NOTNULL;    // methodData pointer
1170   fields[TypeFunc::Parms+1] = TypeInstPtr::BOTTOM;    // receiver oop
1171   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
1172 
1173   // create result type
1174   fields = TypeTuple::fields(1);
1175   fields[TypeFunc::Parms+0] = NULL; // void
1176   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1177   return TypeFunc::make(domain, range);
1178 }
1179 
1180 JRT_LEAF(void, OptoRuntime::profile_receiver_type_C(DataLayout* data, oopDesc* receiver))
1181   if (receiver == NULL) return;
1182   Klass* receiver_klass = receiver->klass();
1183 
1184   intptr_t* mdp = ((intptr_t*)(data)) + DataLayout::header_size_in_cells();
1185   int empty_row = -1;           // free row, if any is encountered
1186 
1187   // ReceiverTypeData* vc = new ReceiverTypeData(mdp);
1188   for (uint row = 0; row < ReceiverTypeData::row_limit(); row++) {
1189     // if (vc->receiver(row) == receiver_klass)
1190     int receiver_off = ReceiverTypeData::receiver_cell_index(row);
1191     intptr_t row_recv = *(mdp + receiver_off);
1192     if (row_recv == (intptr_t) receiver_klass) {
1193       // vc->set_receiver_count(row, vc->receiver_count(row) + DataLayout::counter_increment);
1194       int count_off = ReceiverTypeData::receiver_count_cell_index(row);
1195       *(mdp + count_off) += DataLayout::counter_increment;
1196       return;
1197     } else if (row_recv == 0) {
1198       // else if (vc->receiver(row) == NULL)
1199       empty_row = (int) row;
1200     }
1201   }
1202 
1203   if (empty_row != -1) {
1204     int receiver_off = ReceiverTypeData::receiver_cell_index(empty_row);
1205     // vc->set_receiver(empty_row, receiver_klass);
1206     *(mdp + receiver_off) = (intptr_t) receiver_klass;
1207     // vc->set_receiver_count(empty_row, DataLayout::counter_increment);
1208     int count_off = ReceiverTypeData::receiver_count_cell_index(empty_row);
1209     *(mdp + count_off) = DataLayout::counter_increment;
1210   } else {
1211     // Receiver did not match any saved receiver and there is no empty row for it.
1212     // Increment total counter to indicate polymorphic case.
1213     intptr_t* count_p = (intptr_t*)(((uint8_t*)(data)) + in_bytes(CounterData::count_offset()));
1214     *count_p += DataLayout::counter_increment;
1215   }
1216 JRT_END
1217 
1218 //-------------------------------------------------------------------------------------
1219 // register policy
1220 
1221 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1222   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1223   switch (register_save_policy[reg]) {
1224     case 'C': return false; //SOC
1225     case 'E': return true ; //SOE
1226     case 'N': return false; //NS
1227     case 'A': return false; //AS
1228   }
1229   ShouldNotReachHere();
1230   return false;
1231 }
1232 
1233 //-----------------------------------------------------------------------
1234 // Exceptions
1235 //
1236 
1237 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1238 
1239 // The method is an entry that is always called by a C++ method not
1240 // directly from compiled code. Compiled code will call the C++ method following.
1241 // We can't allow async exception to be installed during  exception processing.
1242 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* thread, nmethod* &nm))
1243 
1244   // Do not confuse exception_oop with pending_exception. The exception_oop
1245   // is only used to pass arguments into the method. Not for general
1246   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1247   // the runtime stubs checks this on exit.
1248   assert(thread->exception_oop() != NULL, "exception oop is found");
1249   address handler_address = NULL;
1250 
1251   Handle exception(thread, thread->exception_oop());
1252   address pc = thread->exception_pc();
1253 
1254   // Clear out the exception oop and pc since looking up an
1255   // exception handler can cause class loading, which might throw an
1256   // exception and those fields are expected to be clear during
1257   // normal bytecode execution.
1258   thread->clear_exception_oop_and_pc();
1259 
1260   LogTarget(Info, exceptions) lt;
1261   if (lt.is_enabled()) {
1262     ResourceMark rm;
1263     LogStream ls(lt);
1264     trace_exception(&ls, exception(), pc, "");
1265   }
1266 
1267   // for AbortVMOnException flag
1268   Exceptions::debug_check_abort(exception);
1269 
1270 #ifdef ASSERT
1271   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1272     // should throw an exception here
1273     ShouldNotReachHere();
1274   }
1275 #endif
1276 
1277   // new exception handling: this method is entered only from adapters
1278   // exceptions from compiled java methods are handled in compiled code
1279   // using rethrow node
1280 
1281   nm = CodeCache::find_nmethod(pc);
1282   assert(nm != NULL, "No NMethod found");
1283   if (nm->is_native_method()) {
1284     fatal("Native method should not have path to exception handling");
1285   } else {
1286     // we are switching to old paradigm: search for exception handler in caller_frame
1287     // instead in exception handler of caller_frame.sender()
1288 
1289     if (JvmtiExport::can_post_on_exceptions()) {
1290       // "Full-speed catching" is not necessary here,
1291       // since we're notifying the VM on every catch.
1292       // Force deoptimization and the rest of the lookup
1293       // will be fine.
1294       deoptimize_caller_frame(thread);
1295     }
1296 
1297     // Check the stack guard pages.  If enabled, look for handler in this frame;
1298     // otherwise, forcibly unwind the frame.
1299     //
1300     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1301     bool force_unwind = !thread->reguard_stack();
1302     bool deopting = false;
1303     if (nm->is_deopt_pc(pc)) {
1304       deopting = true;
1305       RegisterMap map(thread, false);
1306       frame deoptee = thread->last_frame().sender(&map);
1307       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1308       // Adjust the pc back to the original throwing pc
1309       pc = deoptee.pc();
1310     }
1311 
1312     // If we are forcing an unwind because of stack overflow then deopt is
1313     // irrelevant since we are throwing the frame away anyway.
1314 
1315     if (deopting && !force_unwind) {
1316       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1317     } else {
1318 
1319       handler_address =
1320         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
1321 
1322       if (handler_address == NULL) {
1323         bool recursive_exception = false;
1324         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1325         assert (handler_address != NULL, "must have compiled handler");
1326         // Update the exception cache only when the unwind was not forced
1327         // and there didn't happen another exception during the computation of the
1328         // compiled exception handler. Checking for exception oop equality is not
1329         // sufficient because some exceptions are pre-allocated and reused.
1330         if (!force_unwind && !recursive_exception) {
1331           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1332         }
1333       } else {
1334 #ifdef ASSERT
1335         bool recursive_exception = false;
1336         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1337         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1338                  p2i(handler_address), p2i(computed_address));
1339 #endif
1340       }
1341     }
1342 
1343     thread->set_exception_pc(pc);
1344     thread->set_exception_handler_pc(handler_address);
1345 
1346     // Check if the exception PC is a MethodHandle call site.
1347     thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
1348   }
1349 
1350   // Restore correct return pc.  Was saved above.
1351   thread->set_exception_oop(exception());
1352   return handler_address;
1353 
1354 JRT_END
1355 
1356 // We are entering here from exception_blob
1357 // If there is a compiled exception handler in this method, we will continue there;
1358 // otherwise we will unwind the stack and continue at the caller of top frame method
1359 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1360 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1361 // we looked up the handler for has been deoptimized in the meantime. If it has been
1362 // we must not use the handler and instead return the deopt blob.
1363 address OptoRuntime::handle_exception_C(JavaThread* thread) {
1364 //
1365 // We are in Java not VM and in debug mode we have a NoHandleMark
1366 //
1367 #ifndef PRODUCT
1368   SharedRuntime::_find_handler_ctr++;          // find exception handler
1369 #endif
1370   debug_only(NoHandleMark __hm;)
1371   nmethod* nm = NULL;
1372   address handler_address = NULL;
1373   {
1374     // Enter the VM
1375 
1376     ResetNoHandleMark rnhm;
1377     handler_address = handle_exception_C_helper(thread, nm);
1378   }
1379 
1380   // Back in java: Use no oops, DON'T safepoint
1381 
1382   // Now check to see if the handler we are returning is in a now
1383   // deoptimized frame
1384 
1385   if (nm != NULL) {
1386     RegisterMap map(thread, false);
1387     frame caller = thread->last_frame().sender(&map);
1388 #ifdef ASSERT
1389     assert(caller.is_compiled_frame(), "must be");
1390 #endif // ASSERT
1391     if (caller.is_deoptimized_frame()) {
1392       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1393     }
1394   }
1395   return handler_address;
1396 }
1397 
1398 //------------------------------rethrow----------------------------------------
1399 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1400 // is either throwing or rethrowing an exception.  The callee-save registers
1401 // have been restored, synchronized objects have been unlocked and the callee
1402 // stack frame has been removed.  The return address was passed in.
1403 // Exception oop is passed as the 1st argument.  This routine is then called
1404 // from the stub.  On exit, we know where to jump in the caller's code.
1405 // After this C code exits, the stub will pop his frame and end in a jump
1406 // (instead of a return).  We enter the caller's default handler.
1407 //
1408 // This must be JRT_LEAF:
1409 //     - caller will not change its state as we cannot block on exit,
1410 //       therefore raw_exception_handler_for_return_address is all it takes
1411 //       to handle deoptimized blobs
1412 //
1413 // However, there needs to be a safepoint check in the middle!  So compiled
1414 // safepoints are completely watertight.
1415 //
1416 // Thus, it cannot be a leaf since it contains the NoGCVerifier.
1417 //
1418 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1419 //
1420 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1421 #ifndef PRODUCT
1422   SharedRuntime::_rethrow_ctr++;               // count rethrows
1423 #endif
1424   assert (exception != NULL, "should have thrown a NULLPointerException");
1425 #ifdef ASSERT
1426   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1427     // should throw an exception here
1428     ShouldNotReachHere();
1429   }
1430 #endif
1431 
1432   thread->set_vm_result(exception);
1433   // Frame not compiled (handles deoptimization blob)
1434   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1435 }
1436 
1437 
1438 const TypeFunc *OptoRuntime::rethrow_Type() {
1439   // create input type (domain)
1440   const Type **fields = TypeTuple::fields(1);
1441   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1442   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1443 
1444   // create result type (range)
1445   fields = TypeTuple::fields(1);
1446   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1447   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1448 
1449   return TypeFunc::make(domain, range);
1450 }
1451 
1452 
1453 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1454   // Deoptimize the caller before continuing, as the compiled
1455   // exception handler table may not be valid.
1456   if (!StressCompiledExceptionHandlers && doit) {
1457     deoptimize_caller_frame(thread);
1458   }
1459 }
1460 
1461 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1462   // Called from within the owner thread, so no need for safepoint
1463   RegisterMap reg_map(thread);
1464   frame stub_frame = thread->last_frame();
1465   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1466   frame caller_frame = stub_frame.sender(&reg_map);
1467 
1468   // Deoptimize the caller frame.
1469   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1470 }
1471 
1472 
1473 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1474   // Called from within the owner thread, so no need for safepoint
1475   RegisterMap reg_map(thread);
1476   frame stub_frame = thread->last_frame();
1477   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1478   frame caller_frame = stub_frame.sender(&reg_map);
1479   return caller_frame.is_deoptimized_frame();
1480 }
1481 
1482 
1483 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1484   // create input type (domain)
1485   const Type **fields = TypeTuple::fields(1);
1486   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1487   // // The JavaThread* is passed to each routine as the last argument
1488   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1489   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1490 
1491   // create result type (range)
1492   fields = TypeTuple::fields(0);
1493 
1494   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1495 
1496   return TypeFunc::make(domain, range);
1497 }
1498 
1499 
1500 //-----------------------------------------------------------------------------
1501 // Dtrace support.  entry and exit probes have the same signature
1502 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1503   // create input type (domain)
1504   const Type **fields = TypeTuple::fields(2);
1505   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1506   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1507   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1508 
1509   // create result type (range)
1510   fields = TypeTuple::fields(0);
1511 
1512   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1513 
1514   return TypeFunc::make(domain, range);
1515 }
1516 
1517 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1518   // create input type (domain)
1519   const Type **fields = TypeTuple::fields(2);
1520   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1521   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1522 
1523   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1524 
1525   // create result type (range)
1526   fields = TypeTuple::fields(0);
1527 
1528   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1529 
1530   return TypeFunc::make(domain, range);
1531 }
1532 
1533 
1534 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* thread))
1535   assert(oopDesc::is_oop(obj), "must be a valid oop");
1536   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1537   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1538 JRT_END
1539 
1540 //-----------------------------------------------------------------------------
1541 
1542 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1543 
1544 //
1545 // dump the collected NamedCounters.
1546 //
1547 void OptoRuntime::print_named_counters() {
1548   int total_lock_count = 0;
1549   int eliminated_lock_count = 0;
1550 
1551   NamedCounter* c = _named_counters;
1552   while (c) {
1553     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1554       int count = c->count();
1555       if (count > 0) {
1556         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1557         if (Verbose) {
1558           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1559         }
1560         total_lock_count += count;
1561         if (eliminated) {
1562           eliminated_lock_count += count;
1563         }
1564       }
1565     } else if (c->tag() == NamedCounter::BiasedLockingCounter) {
1566       BiasedLockingCounters* blc = ((BiasedLockingNamedCounter*)c)->counters();
1567       if (blc->nonzero()) {
1568         tty->print_cr("%s", c->name());
1569         blc->print_on(tty);
1570       }
1571 #if INCLUDE_RTM_OPT
1572     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1573       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1574       if (rlc->nonzero()) {
1575         tty->print_cr("%s", c->name());
1576         rlc->print_on(tty);
1577       }
1578 #endif
1579     }
1580     c = c->next();
1581   }
1582   if (total_lock_count > 0) {
1583     tty->print_cr("dynamic locks: %d", total_lock_count);
1584     if (eliminated_lock_count) {
1585       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1586                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1587     }
1588   }
1589 }
1590 
1591 //
1592 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1593 //  name which consists of method@line for the inlining tree.
1594 //
1595 
1596 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1597   int max_depth = youngest_jvms->depth();
1598 
1599   // Visit scopes from youngest to oldest.
1600   bool first = true;
1601   stringStream st;
1602   for (int depth = max_depth; depth >= 1; depth--) {
1603     JVMState* jvms = youngest_jvms->of_depth(depth);
1604     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1605     if (!first) {
1606       st.print(" ");
1607     } else {
1608       first = false;
1609     }
1610     int bci = jvms->bci();
1611     if (bci < 0) bci = 0;
1612     if (m != NULL) {
1613       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1614     } else {
1615       st.print("no method");
1616     }
1617     st.print("@%d", bci);
1618     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1619   }
1620   NamedCounter* c;
1621   if (tag == NamedCounter::BiasedLockingCounter) {
1622     c = new BiasedLockingNamedCounter(st.as_string());
1623   } else if (tag == NamedCounter::RTMLockingCounter) {
1624     c = new RTMLockingNamedCounter(st.as_string());
1625   } else {
1626     c = new NamedCounter(st.as_string(), tag);
1627   }
1628 
1629   // atomically add the new counter to the head of the list.  We only
1630   // add counters so this is safe.
1631   NamedCounter* head;
1632   do {
1633     c->set_next(NULL);
1634     head = _named_counters;
1635     c->set_next(head);
1636   } while (Atomic::cmpxchg(c, &_named_counters, head) != head);
1637   return c;
1638 }
1639 
1640 int trace_exception_counter = 0;
1641 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1642   trace_exception_counter++;
1643   stringStream tempst;
1644 
1645   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1646   exception_oop->print_value_on(&tempst);
1647   tempst.print(" in ");
1648   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1649   if (blob->is_compiled()) {
1650     CompiledMethod* cm = blob->as_compiled_method_or_null();
1651     cm->method()->print_value_on(&tempst);
1652   } else if (blob->is_runtime_stub()) {
1653     tempst.print("<runtime-stub>");
1654   } else {
1655     tempst.print("<unknown>");
1656   }
1657   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1658   tempst.print("]");
1659 
1660   st->print_raw_cr(tempst.as_string());
1661 }
1662 
1663 const TypeFunc *OptoRuntime::store_value_type_fields_Type() {
1664   // create input type (domain)
1665   uint total = SharedRuntime::java_return_convention_max_int + SharedRuntime::java_return_convention_max_float*2;
1666   const Type **fields = TypeTuple::fields(total);
1667   // We don't know the number of returned values and their
1668   // types. Assume all registers available to the return convention
1669   // are used.
1670   fields[TypeFunc::Parms] = TypePtr::BOTTOM;
1671   uint i = 1;
1672   for (; i < SharedRuntime::java_return_convention_max_int; i++) {
1673     fields[TypeFunc::Parms+i] = TypeInt::INT;
1674   }
1675   for (; i < total; i+=2) {
1676     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1677     fields[TypeFunc::Parms+i+1] = Type::HALF;
1678   }
1679   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1680 
1681   // create result type (range)
1682   fields = TypeTuple::fields(1);
1683   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1684 
1685   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1686 
1687   return TypeFunc::make(domain, range);
1688 }
1689 
1690 const TypeFunc *OptoRuntime::pack_value_type_Type() {
1691   // create input type (domain)
1692   uint total = 1 + 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] = TypeRawPtr::BOTTOM;
1698   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;
1699   uint i = 2;
1700   for (; i < SharedRuntime::java_return_convention_max_int+1; i++) {
1701     fields[TypeFunc::Parms+i] = TypeInt::INT;
1702   }
1703   for (; i < total; i+=2) {
1704     fields[TypeFunc::Parms+i] = Type::DOUBLE;
1705     fields[TypeFunc::Parms+i+1] = Type::HALF;
1706   }
1707   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + total, fields);
1708 
1709   // create result type (range)
1710   fields = TypeTuple::fields(1);
1711   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;
1712 
1713   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1,fields);
1714 
1715   return TypeFunc::make(domain, range);
1716 }
1717 
1718 JRT_LEAF(void, OptoRuntime::load_unknown_value(valueArrayOopDesc* array, int index, instanceOopDesc* buffer))
1719 {
1720   Klass* klass = array->klass();
1721   assert(klass->is_valueArray_klass(), "expected value array oop");
1722 
1723   ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
1724   ValueKlass* vklass = vaklass->element_klass();
1725   void* src = array->value_at_addr(index, vaklass->layout_helper());
1726   vklass->value_store(src, vklass->data_for_oop(buffer),
1727                         vaklass->element_byte_size(), true, false);
1728 }
1729 JRT_END
1730 
1731 const TypeFunc *OptoRuntime::load_unknown_value_Type() {
1732   // create input type (domain)
1733   const Type **fields = TypeTuple::fields(3);
1734   // We don't know the number of returned values and their
1735   // types. Assume all registers available to the return convention
1736   // are used.
1737   fields[TypeFunc::Parms] = TypeOopPtr::NOTNULL;
1738   fields[TypeFunc::Parms+1] = TypeInt::POS;
1739   fields[TypeFunc::Parms+2] = TypeInstPtr::NOTNULL;
1740 
1741   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1742 
1743   // create result type (range)
1744   fields = TypeTuple::fields(0);
1745   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1746 
1747   return TypeFunc::make(domain, range);
1748 }
1749 
1750 JRT_LEAF(void, OptoRuntime::store_unknown_value(instanceOopDesc* buffer, valueArrayOopDesc* array, int index))
1751 {
1752   assert(buffer != NULL, "can't store null into flat array");
1753   Klass* klass = array->klass();
1754   assert(klass->is_valueArray_klass(), "expected value array");
1755   assert(ArrayKlass::cast(klass)->element_klass() == buffer->klass(), "Store type incorrect");
1756 
1757   ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass);
1758   ValueKlass* vklass = vaklass->element_klass();
1759   const int lh = vaklass->layout_helper();
1760   vklass->value_store(vklass->data_for_oop(buffer), array->value_at_addr(index, lh),
1761                       vaklass->element_byte_size(), true, false);
1762 }
1763 JRT_END
1764 
1765 const TypeFunc *OptoRuntime::store_unknown_value_Type() {
1766   // create input type (domain)
1767   const Type **fields = TypeTuple::fields(3);
1768   // We don't know the number of returned values and their
1769   // types. Assume all registers available to the return convention
1770   // are used.
1771   fields[TypeFunc::Parms] = TypeInstPtr::NOTNULL;
1772   fields[TypeFunc::Parms+1] = TypeOopPtr::NOTNULL;
1773   fields[TypeFunc::Parms+2] = TypeInt::POS;
1774 
1775   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+3, fields);
1776 
1777   // create result type (range)
1778   fields = TypeTuple::fields(0);
1779   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1780 
1781   return TypeFunc::make(domain, range);
1782 }