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