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