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