1 /*
   2  * Copyright 1998-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_runtime.cpp.incl"
  27 
  28 
  29 // For debugging purposes:
  30 //  To force FullGCALot inside a runtime function, add the following two lines
  31 //
  32 //  Universe::release_fullgc_alot_dummy();
  33 //  MarkSweep::invoke(0, "Debugging");
  34 //
  35 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  36 
  37 
  38 
  39 
  40 // Compiled code entry points
  41 address OptoRuntime::_new_instance_Java                           = NULL;
  42 address OptoRuntime::_new_array_Java                              = NULL;
  43 address OptoRuntime::_multianewarray2_Java                        = NULL;
  44 address OptoRuntime::_multianewarray3_Java                        = NULL;
  45 address OptoRuntime::_multianewarray4_Java                        = NULL;
  46 address OptoRuntime::_multianewarray5_Java                        = NULL;
  47 address OptoRuntime::_g1_wb_pre_Java                              = NULL;
  48 address OptoRuntime::_g1_wb_post_Java                             = NULL;
  49 address OptoRuntime::_vtable_must_compile_Java                    = NULL;
  50 address OptoRuntime::_complete_monitor_locking_Java               = NULL;
  51 address OptoRuntime::_rethrow_Java                                = NULL;
  52 
  53 address OptoRuntime::_slow_arraycopy_Java                         = NULL;
  54 address OptoRuntime::_register_finalizer_Java                     = NULL;
  55 
  56 # ifdef ENABLE_ZAP_DEAD_LOCALS
  57 address OptoRuntime::_zap_dead_Java_locals_Java                   = NULL;
  58 address OptoRuntime::_zap_dead_native_locals_Java                 = NULL;
  59 # endif
  60 
  61 
  62 // This should be called in an assertion at the start of OptoRuntime routines
  63 // which are entered from compiled code (all of them)
  64 #ifndef PRODUCT
  65 static bool check_compiled_frame(JavaThread* thread) {
  66   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
  67 #ifdef ASSERT
  68   RegisterMap map(thread, false);
  69   frame caller = thread->last_frame().sender(&map);
  70   assert(caller.is_compiled_frame(), "not being called from compiled like code");
  71 #endif  /* ASSERT */
  72   return true;
  73 }
  74 #endif
  75 
  76 
  77 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, save_arg_regs, return_pc) \
  78   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, save_arg_regs, return_pc)
  79 
  80 void OptoRuntime::generate(ciEnv* env) {
  81 
  82   generate_exception_blob();
  83 
  84   // Note: tls: Means fetching the return oop out of the thread-local storage
  85   //
  86   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,save_args,retpc
  87   // -------------------------------------------------------------------------------------------------------------------------------
  88   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true , false, false);
  89   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true , false, false);
  90   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true , false, false);
  91   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true , false, false);
  92   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true , false, false);
  93   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true , false, false);
  94   gen(env, _g1_wb_pre_Java                 , g1_wb_pre_Type               , SharedRuntime::g1_wb_pre        ,    0 , false, false, false);
  95   gen(env, _g1_wb_post_Java                , g1_wb_post_Type              , SharedRuntime::g1_wb_post       ,    0 , false, false, false);
  96   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C      ,    0 , false, false, false);
  97   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , false, true );
  98 
  99   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false, false);
 100   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false, false);
 101 
 102 # ifdef ENABLE_ZAP_DEAD_LOCALS
 103   gen(env, _zap_dead_Java_locals_Java      , zap_dead_locals_Type         , zap_dead_Java_locals_C          ,    0 , false, true , false );
 104   gen(env, _zap_dead_native_locals_Java    , zap_dead_locals_Type         , zap_dead_native_locals_C        ,    0 , false, true , false );
 105 # endif
 106 
 107 }
 108 
 109 #undef gen
 110 
 111 
 112 // Helper method to do generation of RunTimeStub's
 113 address OptoRuntime::generate_stub( ciEnv* env,
 114                                     TypeFunc_generator gen, address C_function,
 115                                     const char *name, int is_fancy_jump,
 116                                     bool pass_tls,
 117                                     bool save_argument_registers,
 118                                     bool return_pc ) {
 119   ResourceMark rm;
 120   Compile C( env, gen, C_function, name, is_fancy_jump, pass_tls, save_argument_registers, return_pc );
 121   return  C.stub_entry_point();
 122 }
 123 
 124 const char* OptoRuntime::stub_name(address entry) {
 125 #ifndef PRODUCT
 126   CodeBlob* cb = CodeCache::find_blob(entry);
 127   RuntimeStub* rs =(RuntimeStub *)cb;
 128   assert(rs != NULL && rs->is_runtime_stub(), "not a runtime stub");
 129   return rs->name();
 130 #else
 131   // Fast implementation for product mode (maybe it should be inlined too)
 132   return "runtime stub";
 133 #endif
 134 }
 135 
 136 
 137 //=============================================================================
 138 // Opto compiler runtime routines
 139 //=============================================================================
 140 
 141 
 142 //=============================allocation======================================
 143 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 144 // and try allocation again.
 145 
 146 void OptoRuntime::do_eager_card_mark(JavaThread* thread) {
 147   // After any safepoint, just before going back to compiled code,
 148   // we perform a card mark.  This lets the compiled code omit
 149   // card marks for initialization of new objects.
 150   // Keep this code consistent with GraphKit::store_barrier.
 151 
 152   oop new_obj = thread->vm_result();
 153   if (new_obj == NULL)  return;
 154 
 155   assert(Universe::heap()->can_elide_tlab_store_barriers(),
 156          "compiler must check this first");
 157   new_obj = Universe::heap()->new_store_barrier(new_obj);
 158   thread->set_vm_result(new_obj);
 159 }
 160 
 161 // object allocation
 162 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(klassOopDesc* klass, JavaThread* thread))
 163   JRT_BLOCK;
 164 #ifndef PRODUCT
 165   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 166 #endif
 167   assert(check_compiled_frame(thread), "incorrect caller");
 168 
 169   // These checks are cheap to make and support reflective allocation.
 170   int lh = Klass::cast(klass)->layout_helper();
 171   if (Klass::layout_helper_needs_slow_path(lh)
 172       || !instanceKlass::cast(klass)->is_initialized()) {
 173     KlassHandle kh(THREAD, klass);
 174     kh->check_valid_for_instantiation(false, THREAD);
 175     if (!HAS_PENDING_EXCEPTION) {
 176       instanceKlass::cast(kh())->initialize(THREAD);
 177     }
 178     if (!HAS_PENDING_EXCEPTION) {
 179       klass = kh();
 180     } else {
 181       klass = NULL;
 182     }
 183   }
 184 
 185   if (klass != NULL) {
 186     // Scavenge and allocate an instance.
 187     oop result = instanceKlass::cast(klass)->allocate_instance(THREAD);
 188     thread->set_vm_result(result);
 189 
 190     // Pass oops back through thread local storage.  Our apparent type to Java
 191     // is that we return an oop, but we can block on exit from this routine and
 192     // a GC can trash the oop in C's return register.  The generated stub will
 193     // fetch the oop from TLS after any possible GC.
 194   }
 195 
 196   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 197   JRT_BLOCK_END;
 198 
 199   if (GraphKit::use_ReduceInitialCardMarks()) {
 200     // do them now so we don't have to do them on the fast path
 201     do_eager_card_mark(thread);
 202   }
 203 JRT_END
 204 
 205 
 206 // array allocation
 207 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(klassOopDesc* array_type, int len, JavaThread *thread))
 208   JRT_BLOCK;
 209 #ifndef PRODUCT
 210   SharedRuntime::_new_array_ctr++;            // new array requires GC
 211 #endif
 212   assert(check_compiled_frame(thread), "incorrect caller");
 213 
 214   // Scavenge and allocate an instance.
 215   oop result;
 216 
 217   if (Klass::cast(array_type)->oop_is_typeArray()) {
 218     // The oopFactory likes to work with the element type.
 219     // (We could bypass the oopFactory, since it doesn't add much value.)
 220     BasicType elem_type = typeArrayKlass::cast(array_type)->element_type();
 221     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 222   } else {
 223     // Although the oopFactory likes to work with the elem_type,
 224     // the compiler prefers the array_type, since it must already have
 225     // that latter value in hand for the fast path.
 226     klassOopDesc* elem_type = objArrayKlass::cast(array_type)->element_klass();
 227     result = oopFactory::new_objArray(elem_type, len, THREAD);
 228   }
 229 
 230   // Pass oops back through thread local storage.  Our apparent type to Java
 231   // is that we return an oop, but we can block on exit from this routine and
 232   // a GC can trash the oop in C's return register.  The generated stub will
 233   // fetch the oop from TLS after any possible GC.
 234   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 235   thread->set_vm_result(result);
 236   JRT_BLOCK_END;
 237 
 238   if (GraphKit::use_ReduceInitialCardMarks()) {
 239     // do them now so we don't have to do them on the fast path
 240     do_eager_card_mark(thread);
 241   }
 242 JRT_END
 243 
 244 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 245 
 246 // multianewarray for 2 dimensions
 247 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(klassOopDesc* elem_type, int len1, int len2, JavaThread *thread))
 248 #ifndef PRODUCT
 249   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 250 #endif
 251   assert(check_compiled_frame(thread), "incorrect caller");
 252   assert(oop(elem_type)->is_klass(), "not a class");
 253   jint dims[2];
 254   dims[0] = len1;
 255   dims[1] = len2;
 256   oop obj = arrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 257   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 258   thread->set_vm_result(obj);
 259 JRT_END
 260 
 261 // multianewarray for 3 dimensions
 262 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(klassOopDesc* elem_type, int len1, int len2, int len3, JavaThread *thread))
 263 #ifndef PRODUCT
 264   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 265 #endif
 266   assert(check_compiled_frame(thread), "incorrect caller");
 267   assert(oop(elem_type)->is_klass(), "not a class");
 268   jint dims[3];
 269   dims[0] = len1;
 270   dims[1] = len2;
 271   dims[2] = len3;
 272   oop obj = arrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 273   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 274   thread->set_vm_result(obj);
 275 JRT_END
 276 
 277 // multianewarray for 4 dimensions
 278 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(klassOopDesc* elem_type, int len1, int len2, int len3, int len4, JavaThread *thread))
 279 #ifndef PRODUCT
 280   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 281 #endif
 282   assert(check_compiled_frame(thread), "incorrect caller");
 283   assert(oop(elem_type)->is_klass(), "not a class");
 284   jint dims[4];
 285   dims[0] = len1;
 286   dims[1] = len2;
 287   dims[2] = len3;
 288   dims[3] = len4;
 289   oop obj = arrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 290   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 291   thread->set_vm_result(obj);
 292 JRT_END
 293 
 294 // multianewarray for 5 dimensions
 295 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(klassOopDesc* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread *thread))
 296 #ifndef PRODUCT
 297   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 298 #endif
 299   assert(check_compiled_frame(thread), "incorrect caller");
 300   assert(oop(elem_type)->is_klass(), "not a class");
 301   jint dims[5];
 302   dims[0] = len1;
 303   dims[1] = len2;
 304   dims[2] = len3;
 305   dims[3] = len4;
 306   dims[4] = len5;
 307   oop obj = arrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 308   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
 309   thread->set_vm_result(obj);
 310 JRT_END
 311 
 312 const TypeFunc *OptoRuntime::new_instance_Type() {
 313   // create input type (domain)
 314   const Type **fields = TypeTuple::fields(1);
 315   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 316   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 317 
 318   // create result type (range)
 319   fields = TypeTuple::fields(1);
 320   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 321 
 322   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 323 
 324   return TypeFunc::make(domain, range);
 325 }
 326 
 327 
 328 const TypeFunc *OptoRuntime::athrow_Type() {
 329   // create input type (domain)
 330   const Type **fields = TypeTuple::fields(1);
 331   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
 332   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 333 
 334   // create result type (range)
 335   fields = TypeTuple::fields(0);
 336 
 337   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 338 
 339   return TypeFunc::make(domain, range);
 340 }
 341 
 342 
 343 const TypeFunc *OptoRuntime::new_array_Type() {
 344   // create input type (domain)
 345   const Type **fields = TypeTuple::fields(2);
 346   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 347   fields[TypeFunc::Parms+1] = TypeInt::INT;       // array size
 348   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 349 
 350   // create result type (range)
 351   fields = TypeTuple::fields(1);
 352   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 353 
 354   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 355 
 356   return TypeFunc::make(domain, range);
 357 }
 358 
 359 const TypeFunc *OptoRuntime::multianewarray_Type(int ndim) {
 360   // create input type (domain)
 361   const int nargs = ndim + 1;
 362   const Type **fields = TypeTuple::fields(nargs);
 363   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
 364   for( int i = 1; i < nargs; i++ )
 365     fields[TypeFunc::Parms + i] = TypeInt::INT;       // array size
 366   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+nargs, fields);
 367 
 368   // create result type (range)
 369   fields = TypeTuple::fields(1);
 370   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
 371   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 372 
 373   return TypeFunc::make(domain, range);
 374 }
 375 
 376 const TypeFunc *OptoRuntime::multianewarray2_Type() {
 377   return multianewarray_Type(2);
 378 }
 379 
 380 const TypeFunc *OptoRuntime::multianewarray3_Type() {
 381   return multianewarray_Type(3);
 382 }
 383 
 384 const TypeFunc *OptoRuntime::multianewarray4_Type() {
 385   return multianewarray_Type(4);
 386 }
 387 
 388 const TypeFunc *OptoRuntime::multianewarray5_Type() {
 389   return multianewarray_Type(5);
 390 }
 391 
 392 const TypeFunc *OptoRuntime::g1_wb_pre_Type() {
 393   const Type **fields = TypeTuple::fields(2);
 394   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value
 395   fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread
 396   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 397 
 398   // create result type (range)
 399   fields = TypeTuple::fields(0);
 400   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 401 
 402   return TypeFunc::make(domain, range);
 403 }
 404 
 405 const TypeFunc *OptoRuntime::g1_wb_post_Type() {
 406 
 407   const Type **fields = TypeTuple::fields(2);
 408   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL;  // Card addr
 409   fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // thread
 410   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 411 
 412   // create result type (range)
 413   fields = TypeTuple::fields(0);
 414   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 415 
 416   return TypeFunc::make(domain, range);
 417 }
 418 
 419 const TypeFunc *OptoRuntime::uncommon_trap_Type() {
 420   // create input type (domain)
 421   const Type **fields = TypeTuple::fields(1);
 422   // symbolOop name of class to be loaded
 423   fields[TypeFunc::Parms+0] = TypeInt::INT;
 424   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 425 
 426   // create result type (range)
 427   fields = TypeTuple::fields(0);
 428   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
 429 
 430   return TypeFunc::make(domain, range);
 431 }
 432 
 433 # ifdef ENABLE_ZAP_DEAD_LOCALS
 434 // Type used for stub generation for zap_dead_locals.
 435 // No inputs or outputs
 436 const TypeFunc *OptoRuntime::zap_dead_locals_Type() {
 437   // create input type (domain)
 438   const Type **fields = TypeTuple::fields(0);
 439   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms,fields);
 440 
 441   // create result type (range)
 442   fields = TypeTuple::fields(0);
 443   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms,fields);
 444 
 445   return TypeFunc::make(domain,range);
 446 }
 447 # endif
 448 
 449 
 450 //-----------------------------------------------------------------------------
 451 // Monitor Handling
 452 const TypeFunc *OptoRuntime::complete_monitor_enter_Type() {
 453   // create input type (domain)
 454   const Type **fields = TypeTuple::fields(2);
 455   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 456   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // Address of stack location for lock
 457   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 458 
 459   // create result type (range)
 460   fields = TypeTuple::fields(0);
 461 
 462   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
 463 
 464   return TypeFunc::make(domain,range);
 465 }
 466 
 467 
 468 //-----------------------------------------------------------------------------
 469 const TypeFunc *OptoRuntime::complete_monitor_exit_Type() {
 470   // create input type (domain)
 471   const Type **fields = TypeTuple::fields(2);
 472   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
 473   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // Address of stack location for lock
 474   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
 475 
 476   // create result type (range)
 477   fields = TypeTuple::fields(0);
 478 
 479   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
 480 
 481   return TypeFunc::make(domain,range);
 482 }
 483 
 484 const TypeFunc* OptoRuntime::flush_windows_Type() {
 485   // create input type (domain)
 486   const Type** fields = TypeTuple::fields(1);
 487   fields[TypeFunc::Parms+0] = NULL; // void
 488   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
 489 
 490   // create result type
 491   fields = TypeTuple::fields(1);
 492   fields[TypeFunc::Parms+0] = NULL; // void
 493   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 494 
 495   return TypeFunc::make(domain, range);
 496 }
 497 
 498 const TypeFunc* OptoRuntime::l2f_Type() {
 499   // create input type (domain)
 500   const Type **fields = TypeTuple::fields(2);
 501   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 502   fields[TypeFunc::Parms+1] = Type::HALF;
 503   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 504 
 505   // create result type (range)
 506   fields = TypeTuple::fields(1);
 507   fields[TypeFunc::Parms+0] = Type::FLOAT;
 508   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 509 
 510   return TypeFunc::make(domain, range);
 511 }
 512 
 513 const TypeFunc* OptoRuntime::modf_Type() {
 514   const Type **fields = TypeTuple::fields(2);
 515   fields[TypeFunc::Parms+0] = Type::FLOAT;
 516   fields[TypeFunc::Parms+1] = Type::FLOAT;
 517   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 518 
 519   // create result type (range)
 520   fields = TypeTuple::fields(1);
 521   fields[TypeFunc::Parms+0] = Type::FLOAT;
 522 
 523   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 524 
 525   return TypeFunc::make(domain, range);
 526 }
 527 
 528 const TypeFunc *OptoRuntime::Math_D_D_Type() {
 529   // create input type (domain)
 530   const Type **fields = TypeTuple::fields(2);
 531   // symbolOop name of class to be loaded
 532   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 533   fields[TypeFunc::Parms+1] = Type::HALF;
 534   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 535 
 536   // create result type (range)
 537   fields = TypeTuple::fields(2);
 538   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 539   fields[TypeFunc::Parms+1] = Type::HALF;
 540   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 541 
 542   return TypeFunc::make(domain, range);
 543 }
 544 
 545 const TypeFunc* OptoRuntime::Math_DD_D_Type() {
 546   const Type **fields = TypeTuple::fields(4);
 547   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 548   fields[TypeFunc::Parms+1] = Type::HALF;
 549   fields[TypeFunc::Parms+2] = Type::DOUBLE;
 550   fields[TypeFunc::Parms+3] = Type::HALF;
 551   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+4, fields);
 552 
 553   // create result type (range)
 554   fields = TypeTuple::fields(2);
 555   fields[TypeFunc::Parms+0] = Type::DOUBLE;
 556   fields[TypeFunc::Parms+1] = Type::HALF;
 557   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 558 
 559   return TypeFunc::make(domain, range);
 560 }
 561 
 562 //-------------- currentTimeMillis
 563 
 564 const TypeFunc* OptoRuntime::current_time_millis_Type() {
 565   // create input type (domain)
 566   const Type **fields = TypeTuple::fields(0);
 567   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
 568 
 569   // create result type (range)
 570   fields = TypeTuple::fields(2);
 571   fields[TypeFunc::Parms+0] = TypeLong::LONG;
 572   fields[TypeFunc::Parms+1] = Type::HALF;
 573   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
 574 
 575   return TypeFunc::make(domain, range);
 576 }
 577 
 578 // arraycopy stub variations:
 579 enum ArrayCopyType {
 580   ac_fast,                      // void(ptr, ptr, size_t)
 581   ac_checkcast,                 //  int(ptr, ptr, size_t, size_t, ptr)
 582   ac_slow,                      // void(ptr, int, ptr, int, int)
 583   ac_generic                    //  int(ptr, int, ptr, int, int)
 584 };
 585 
 586 static const TypeFunc* make_arraycopy_Type(ArrayCopyType act) {
 587   // create input type (domain)
 588   int num_args      = (act == ac_fast ? 3 : 5);
 589   int num_size_args = (act == ac_fast ? 1 : act == ac_checkcast ? 2 : 0);
 590   int argcnt = num_args;
 591   LP64_ONLY(argcnt += num_size_args); // halfwords for lengths
 592   const Type** fields = TypeTuple::fields(argcnt);
 593   int argp = TypeFunc::Parms;
 594   fields[argp++] = TypePtr::NOTNULL;    // src
 595   if (num_size_args == 0) {
 596     fields[argp++] = TypeInt::INT;      // src_pos
 597   }
 598   fields[argp++] = TypePtr::NOTNULL;    // dest
 599   if (num_size_args == 0) {
 600     fields[argp++] = TypeInt::INT;      // dest_pos
 601     fields[argp++] = TypeInt::INT;      // length
 602   }
 603   while (num_size_args-- > 0) {
 604     fields[argp++] = TypeX_X;               // size in whatevers (size_t)
 605     LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
 606   }
 607   if (act == ac_checkcast) {
 608     fields[argp++] = TypePtr::NOTNULL;  // super_klass
 609   }
 610   assert(argp == TypeFunc::Parms+argcnt, "correct decoding of act");
 611   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
 612 
 613   // create result type if needed
 614   int retcnt = (act == ac_checkcast || act == ac_generic ? 1 : 0);
 615   fields = TypeTuple::fields(1);
 616   if (retcnt == 0)
 617     fields[TypeFunc::Parms+0] = NULL; // void
 618   else
 619     fields[TypeFunc::Parms+0] = TypeInt::INT; // status result, if needed
 620   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+retcnt, fields);
 621   return TypeFunc::make(domain, range);
 622 }
 623 
 624 const TypeFunc* OptoRuntime::fast_arraycopy_Type() {
 625   // This signature is simple:  Two base pointers and a size_t.
 626   return make_arraycopy_Type(ac_fast);
 627 }
 628 
 629 const TypeFunc* OptoRuntime::checkcast_arraycopy_Type() {
 630   // An extension of fast_arraycopy_Type which adds type checking.
 631   return make_arraycopy_Type(ac_checkcast);
 632 }
 633 
 634 const TypeFunc* OptoRuntime::slow_arraycopy_Type() {
 635   // This signature is exactly the same as System.arraycopy.
 636   // There are no intptr_t (int/long) arguments.
 637   return make_arraycopy_Type(ac_slow);
 638 }
 639 
 640 const TypeFunc* OptoRuntime::generic_arraycopy_Type() {
 641   // This signature is like System.arraycopy, except that it returns status.
 642   return make_arraycopy_Type(ac_generic);
 643 }
 644 
 645 
 646 //------------- Interpreter state access for on stack replacement
 647 const TypeFunc* OptoRuntime::osr_end_Type() {
 648   // create input type (domain)
 649   const Type **fields = TypeTuple::fields(1);
 650   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
 651   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
 652 
 653   // create result type
 654   fields = TypeTuple::fields(1);
 655   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
 656   fields[TypeFunc::Parms+0] = NULL; // void
 657   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 658   return TypeFunc::make(domain, range);
 659 }
 660 
 661 //-------------- methodData update helpers
 662 
 663 const TypeFunc* OptoRuntime::profile_receiver_type_Type() {
 664   // create input type (domain)
 665   const Type **fields = TypeTuple::fields(2);
 666   fields[TypeFunc::Parms+0] = TypeAryPtr::NOTNULL;    // methodData pointer
 667   fields[TypeFunc::Parms+1] = TypeInstPtr::BOTTOM;    // receiver oop
 668   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
 669 
 670   // create result type
 671   fields = TypeTuple::fields(1);
 672   fields[TypeFunc::Parms+0] = NULL; // void
 673   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
 674   return TypeFunc::make(domain,range);
 675 }
 676 
 677 JRT_LEAF(void, OptoRuntime::profile_receiver_type_C(DataLayout* data, oopDesc* receiver))
 678   if (receiver == NULL) return;
 679   klassOop receiver_klass = receiver->klass();
 680 
 681   intptr_t* mdp = ((intptr_t*)(data)) + DataLayout::header_size_in_cells();
 682   int empty_row = -1;           // free row, if any is encountered
 683 
 684   // ReceiverTypeData* vc = new ReceiverTypeData(mdp);
 685   for (uint row = 0; row < ReceiverTypeData::row_limit(); row++) {
 686     // if (vc->receiver(row) == receiver_klass)
 687     int receiver_off = ReceiverTypeData::receiver_cell_index(row);
 688     intptr_t row_recv = *(mdp + receiver_off);
 689     if (row_recv == (intptr_t) receiver_klass) {
 690       // vc->set_receiver_count(row, vc->receiver_count(row) + DataLayout::counter_increment);
 691       int count_off = ReceiverTypeData::receiver_count_cell_index(row);
 692       *(mdp + count_off) += DataLayout::counter_increment;
 693       return;
 694     } else if (row_recv == 0) {
 695       // else if (vc->receiver(row) == NULL)
 696       empty_row = (int) row;
 697     }
 698   }
 699 
 700   if (empty_row != -1) {
 701     int receiver_off = ReceiverTypeData::receiver_cell_index(empty_row);
 702     // vc->set_receiver(empty_row, receiver_klass);
 703     *(mdp + receiver_off) = (intptr_t) receiver_klass;
 704     // vc->set_receiver_count(empty_row, DataLayout::counter_increment);
 705     int count_off = ReceiverTypeData::receiver_count_cell_index(empty_row);
 706     *(mdp + count_off) = DataLayout::counter_increment;
 707   }
 708 JRT_END
 709 
 710 //-----------------------------------------------------------------------------
 711 // implicit exception support.
 712 
 713 static void report_null_exception_in_code_cache(address exception_pc) {
 714   ResourceMark rm;
 715   CodeBlob* n = CodeCache::find_blob(exception_pc);
 716   if (n != NULL) {
 717     tty->print_cr("#");
 718     tty->print_cr("# HotSpot Runtime Error, null exception in generated code");
 719     tty->print_cr("#");
 720     tty->print_cr("# pc where exception happened = " INTPTR_FORMAT, exception_pc);
 721 
 722     if (n->is_nmethod()) {
 723       methodOop method = ((nmethod*)n)->method();
 724       tty->print_cr("# Method where it happened %s.%s ", Klass::cast(method->method_holder())->name()->as_C_string(), method->name()->as_C_string());
 725       tty->print_cr("#");
 726       if (ShowMessageBoxOnError && UpdateHotSpotCompilerFileOnError) {
 727         const char* title    = "HotSpot Runtime Error";
 728         const char* question = "Do you want to exclude compilation of this method in future runs?";
 729         if (os::message_box(title, question)) {
 730           CompilerOracle::append_comment_to_file("");
 731           CompilerOracle::append_comment_to_file("Null exception in compiled code resulted in the following exclude");
 732           CompilerOracle::append_comment_to_file("");
 733           CompilerOracle::append_exclude_to_file(method);
 734           tty->print_cr("#");
 735           tty->print_cr("# %s has been updated to exclude the specified method", CompileCommandFile);
 736           tty->print_cr("#");
 737         }
 738       }
 739       fatal("Implicit null exception happened in compiled method");
 740     } else {
 741       n->print();
 742       fatal("Implicit null exception happened in generated stub");
 743     }
 744   }
 745   fatal("Implicit null exception at wrong place");
 746 }
 747 
 748 
 749 //-------------------------------------------------------------------------------------
 750 // register policy
 751 
 752 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
 753   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
 754   switch (register_save_policy[reg]) {
 755     case 'C': return false; //SOC
 756     case 'E': return true ; //SOE
 757     case 'N': return false; //NS
 758     case 'A': return false; //AS
 759   }
 760   ShouldNotReachHere();
 761   return false;
 762 }
 763 
 764 //-----------------------------------------------------------------------
 765 // Exceptions
 766 //
 767 
 768 static void trace_exception(oop exception_oop, address exception_pc, const char* msg) PRODUCT_RETURN;
 769 
 770 // The method is an entry that is always called by a C++ method not
 771 // directly from compiled code. Compiled code will call the C++ method following.
 772 // We can't allow async exception to be installed during  exception processing.
 773 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* thread, nmethod* &nm))
 774 
 775   // Do not confuse exception_oop with pending_exception. The exception_oop
 776   // is only used to pass arguments into the method. Not for general
 777   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
 778   // the runtime stubs checks this on exit.
 779   assert(thread->exception_oop() != NULL, "exception oop is found");
 780   address handler_address = NULL;
 781 
 782   Handle exception(thread, thread->exception_oop());
 783 
 784   if (TraceExceptions) {
 785     trace_exception(exception(), thread->exception_pc(), "");
 786   }
 787   // for AbortVMOnException flag
 788   NOT_PRODUCT(Exceptions::debug_check_abort(exception));
 789 
 790   #ifdef ASSERT
 791     if (!(exception->is_a(SystemDictionary::throwable_klass()))) {
 792       // should throw an exception here
 793       ShouldNotReachHere();
 794     }
 795   #endif
 796 
 797 
 798   // new exception handling: this method is entered only from adapters
 799   // exceptions from compiled java methods are handled in compiled code
 800   // using rethrow node
 801 
 802   address pc = thread->exception_pc();
 803   nm = CodeCache::find_nmethod(pc);
 804   assert(nm != NULL, "No NMethod found");
 805   if (nm->is_native_method()) {
 806     fatal("Native mathod should not have path to exception handling");
 807   } else {
 808     // we are switching to old paradigm: search for exception handler in caller_frame
 809     // instead in exception handler of caller_frame.sender()
 810 
 811     if (JvmtiExport::can_post_exceptions()) {
 812       // "Full-speed catching" is not necessary here,
 813       // since we're notifying the VM on every catch.
 814       // Force deoptimization and the rest of the lookup
 815       // will be fine.
 816       deoptimize_caller_frame(thread, true);
 817     }
 818 
 819     // Check the stack guard pages.  If enabled, look for handler in this frame;
 820     // otherwise, forcibly unwind the frame.
 821     //
 822     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
 823     bool force_unwind = !thread->reguard_stack();
 824     bool deopting = false;
 825     if (nm->is_deopt_pc(pc)) {
 826       deopting = true;
 827       RegisterMap map(thread, false);
 828       frame deoptee = thread->last_frame().sender(&map);
 829       assert(deoptee.is_deoptimized_frame(), "must be deopted");
 830       // Adjust the pc back to the original throwing pc
 831       pc = deoptee.pc();
 832     }
 833 
 834     // If we are forcing an unwind because of stack overflow then deopt is
 835     // irrelevant sice we are throwing the frame away anyway.
 836 
 837     if (deopting && !force_unwind) {
 838       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
 839     } else {
 840 
 841       handler_address =
 842         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
 843 
 844       if (handler_address == NULL) {
 845         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true);
 846         assert (handler_address != NULL, "must have compiled handler");
 847         // Update the exception cache only when the unwind was not forced.
 848         if (!force_unwind) {
 849           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
 850         }
 851       } else {
 852         assert(handler_address == SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true), "Must be the same");
 853       }
 854     }
 855 
 856     thread->set_exception_pc(pc);
 857     thread->set_exception_handler_pc(handler_address);
 858     thread->set_exception_stack_size(0);
 859   }
 860 
 861   // Restore correct return pc.  Was saved above.
 862   thread->set_exception_oop(exception());
 863   return handler_address;
 864 
 865 JRT_END
 866 
 867 // We are entering here from exception_blob
 868 // If there is a compiled exception handler in this method, we will continue there;
 869 // otherwise we will unwind the stack and continue at the caller of top frame method
 870 // Note we enter without the usual JRT wrapper. We will call a helper routine that
 871 // will do the normal VM entry. We do it this way so that we can see if the nmethod
 872 // we looked up the handler for has been deoptimized in the meantime. If it has been
 873 // we must not use the handler and instread return the deopt blob.
 874 address OptoRuntime::handle_exception_C(JavaThread* thread) {
 875 //
 876 // We are in Java not VM and in debug mode we have a NoHandleMark
 877 //
 878 #ifndef PRODUCT
 879   SharedRuntime::_find_handler_ctr++;          // find exception handler
 880 #endif
 881   debug_only(NoHandleMark __hm;)
 882   nmethod* nm = NULL;
 883   address handler_address = NULL;
 884   {
 885     // Enter the VM
 886 
 887     ResetNoHandleMark rnhm;
 888     handler_address = handle_exception_C_helper(thread, nm);
 889   }
 890 
 891   // Back in java: Use no oops, DON'T safepoint
 892 
 893   // Now check to see if the handler we are returning is in a now
 894   // deoptimized frame
 895 
 896   if (nm != NULL) {
 897     RegisterMap map(thread, false);
 898     frame caller = thread->last_frame().sender(&map);
 899 #ifdef ASSERT
 900     assert(caller.is_compiled_frame(), "must be");
 901 #endif // ASSERT
 902     if (caller.is_deoptimized_frame()) {
 903       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
 904     }
 905   }
 906   return handler_address;
 907 }
 908 
 909 //------------------------------rethrow----------------------------------------
 910 // We get here after compiled code has executed a 'RethrowNode'.  The callee
 911 // is either throwing or rethrowing an exception.  The callee-save registers
 912 // have been restored, synchronized objects have been unlocked and the callee
 913 // stack frame has been removed.  The return address was passed in.
 914 // Exception oop is passed as the 1st argument.  This routine is then called
 915 // from the stub.  On exit, we know where to jump in the caller's code.
 916 // After this C code exits, the stub will pop his frame and end in a jump
 917 // (instead of a return).  We enter the caller's default handler.
 918 //
 919 // This must be JRT_LEAF:
 920 //     - caller will not change its state as we cannot block on exit,
 921 //       therefore raw_exception_handler_for_return_address is all it takes
 922 //       to handle deoptimized blobs
 923 //
 924 // However, there needs to be a safepoint check in the middle!  So compiled
 925 // safepoints are completely watertight.
 926 //
 927 // Thus, it cannot be a leaf since it contains the No_GC_Verifier.
 928 //
 929 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
 930 //
 931 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
 932 #ifndef PRODUCT
 933   SharedRuntime::_rethrow_ctr++;               // count rethrows
 934 #endif
 935   assert (exception != NULL, "should have thrown a NULLPointerException");
 936 #ifdef ASSERT
 937   if (!(exception->is_a(SystemDictionary::throwable_klass()))) {
 938     // should throw an exception here
 939     ShouldNotReachHere();
 940   }
 941 #endif
 942 
 943   thread->set_vm_result(exception);
 944   // Frame not compiled (handles deoptimization blob)
 945   return SharedRuntime::raw_exception_handler_for_return_address(ret_pc);
 946 }
 947 
 948 
 949 const TypeFunc *OptoRuntime::rethrow_Type() {
 950   // create input type (domain)
 951   const Type **fields = TypeTuple::fields(1);
 952   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
 953   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
 954 
 955   // create result type (range)
 956   fields = TypeTuple::fields(1);
 957   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
 958   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
 959 
 960   return TypeFunc::make(domain, range);
 961 }
 962 
 963 
 964 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
 965   // Deoptimize frame
 966   if (doit) {
 967     // Called from within the owner thread, so no need for safepoint
 968     RegisterMap reg_map(thread);
 969     frame stub_frame = thread->last_frame();
 970     assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
 971     frame caller_frame = stub_frame.sender(&reg_map);
 972 
 973     VM_DeoptimizeFrame deopt(thread, caller_frame.id());
 974     VMThread::execute(&deopt);
 975   }
 976 }
 977 
 978 
 979 const TypeFunc *OptoRuntime::register_finalizer_Type() {
 980   // create input type (domain)
 981   const Type **fields = TypeTuple::fields(1);
 982   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
 983   // // The JavaThread* is passed to each routine as the last argument
 984   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
 985   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
 986 
 987   // create result type (range)
 988   fields = TypeTuple::fields(0);
 989 
 990   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
 991 
 992   return TypeFunc::make(domain,range);
 993 }
 994 
 995 
 996 //-----------------------------------------------------------------------------
 997 // Dtrace support.  entry and exit probes have the same signature
 998 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
 999   // create input type (domain)
1000   const Type **fields = TypeTuple::fields(2);
1001   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1002   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // methodOop;    Method we are entering
1003   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1004 
1005   // create result type (range)
1006   fields = TypeTuple::fields(0);
1007 
1008   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1009 
1010   return TypeFunc::make(domain,range);
1011 }
1012 
1013 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1014   // create input type (domain)
1015   const Type **fields = TypeTuple::fields(2);
1016   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1017   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1018 
1019   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1020 
1021   // create result type (range)
1022   fields = TypeTuple::fields(0);
1023 
1024   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1025 
1026   return TypeFunc::make(domain,range);
1027 }
1028 
1029 
1030 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* thread))
1031   assert(obj->is_oop(), "must be a valid oop");
1032   assert(obj->klass()->klass_part()->has_finalizer(), "shouldn't be here otherwise");
1033   instanceKlass::register_finalizer(instanceOop(obj), CHECK);
1034 JRT_END
1035 
1036 //-----------------------------------------------------------------------------
1037 
1038 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1039 
1040 //
1041 // dump the collected NamedCounters.
1042 //
1043 void OptoRuntime::print_named_counters() {
1044   int total_lock_count = 0;
1045   int eliminated_lock_count = 0;
1046 
1047   NamedCounter* c = _named_counters;
1048   while (c) {
1049     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1050       int count = c->count();
1051       if (count > 0) {
1052         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1053         if (Verbose) {
1054           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1055         }
1056         total_lock_count += count;
1057         if (eliminated) {
1058           eliminated_lock_count += count;
1059         }
1060       }
1061     } else if (c->tag() == NamedCounter::BiasedLockingCounter) {
1062       BiasedLockingCounters* blc = ((BiasedLockingNamedCounter*)c)->counters();
1063       if (blc->nonzero()) {
1064         tty->print_cr("%s", c->name());
1065         blc->print_on(tty);
1066       }
1067     }
1068     c = c->next();
1069   }
1070   if (total_lock_count > 0) {
1071     tty->print_cr("dynamic locks: %d", total_lock_count);
1072     if (eliminated_lock_count) {
1073       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1074                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1075     }
1076   }
1077 }
1078 
1079 //
1080 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1081 //  name which consists of method@line for the inlining tree.
1082 //
1083 
1084 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1085   int max_depth = youngest_jvms->depth();
1086 
1087   // Visit scopes from youngest to oldest.
1088   bool first = true;
1089   stringStream st;
1090   for (int depth = max_depth; depth >= 1; depth--) {
1091     JVMState* jvms = youngest_jvms->of_depth(depth);
1092     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1093     if (!first) {
1094       st.print(" ");
1095     } else {
1096       first = false;
1097     }
1098     int bci = jvms->bci();
1099     if (bci < 0) bci = 0;
1100     st.print("%s.%s@%d", m->holder()->name()->as_utf8(), m->name()->as_utf8(), bci);
1101     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1102   }
1103   NamedCounter* c;
1104   if (tag == NamedCounter::BiasedLockingCounter) {
1105     c = new BiasedLockingNamedCounter(strdup(st.as_string()));
1106   } else {
1107     c = new NamedCounter(strdup(st.as_string()), tag);
1108   }
1109 
1110   // atomically add the new counter to the head of the list.  We only
1111   // add counters so this is safe.
1112   NamedCounter* head;
1113   do {
1114     head = _named_counters;
1115     c->set_next(head);
1116   } while (Atomic::cmpxchg_ptr(c, &_named_counters, head) != head);
1117   return c;
1118 }
1119 
1120 //-----------------------------------------------------------------------------
1121 // Non-product code
1122 #ifndef PRODUCT
1123 
1124 int trace_exception_counter = 0;
1125 static void trace_exception(oop exception_oop, address exception_pc, const char* msg) {
1126   ttyLocker ttyl;
1127   trace_exception_counter++;
1128   tty->print("%d [Exception (%s): ", trace_exception_counter, msg);
1129   exception_oop->print_value();
1130   tty->print(" in ");
1131   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1132   if (blob->is_nmethod()) {
1133     ((nmethod*)blob)->method()->print_value();
1134   } else if (blob->is_runtime_stub()) {
1135     tty->print("<runtime-stub>");
1136   } else {
1137     tty->print("<unknown>");
1138   }
1139   tty->print(" at " INTPTR_FORMAT,  exception_pc);
1140   tty->print_cr("]");
1141 }
1142 
1143 #endif  // PRODUCT
1144 
1145 
1146 # ifdef ENABLE_ZAP_DEAD_LOCALS
1147 // Called from call sites in compiled code with oop maps (actually safepoints)
1148 // Zaps dead locals in first java frame.
1149 // Is entry because may need to lock to generate oop maps
1150 // Currently, only used for compiler frames, but someday may be used
1151 // for interpreter frames, too.
1152 
1153 int OptoRuntime::ZapDeadCompiledLocals_count = 0;
1154 
1155 // avoid pointers to member funcs with these helpers
1156 static bool is_java_frame(  frame* f) { return f->is_java_frame();   }
1157 static bool is_native_frame(frame* f) { return f->is_native_frame(); }
1158 
1159 
1160 void OptoRuntime::zap_dead_java_or_native_locals(JavaThread* thread,
1161                                                 bool (*is_this_the_right_frame_to_zap)(frame*)) {
1162   assert(JavaThread::current() == thread, "is this needed?");
1163 
1164   if ( !ZapDeadCompiledLocals )  return;
1165 
1166   bool skip = false;
1167 
1168        if ( ZapDeadCompiledLocalsFirst  ==  0  ) ; // nothing special
1169   else if ( ZapDeadCompiledLocalsFirst  >  ZapDeadCompiledLocals_count )  skip = true;
1170   else if ( ZapDeadCompiledLocalsFirst  == ZapDeadCompiledLocals_count )
1171     warning("starting zapping after skipping");
1172 
1173        if ( ZapDeadCompiledLocalsLast  ==  -1  ) ; // nothing special
1174   else if ( ZapDeadCompiledLocalsLast  <   ZapDeadCompiledLocals_count )  skip = true;
1175   else if ( ZapDeadCompiledLocalsLast  ==  ZapDeadCompiledLocals_count )
1176     warning("about to zap last zap");
1177 
1178   ++ZapDeadCompiledLocals_count; // counts skipped zaps, too
1179 
1180   if ( skip )  return;
1181 
1182   // find java frame and zap it
1183 
1184   for (StackFrameStream sfs(thread);  !sfs.is_done();  sfs.next()) {
1185     if (is_this_the_right_frame_to_zap(sfs.current()) ) {
1186       sfs.current()->zap_dead_locals(thread, sfs.register_map());
1187       return;
1188     }
1189   }
1190   warning("no frame found to zap in zap_dead_Java_locals_C");
1191 }
1192 
1193 JRT_LEAF(void, OptoRuntime::zap_dead_Java_locals_C(JavaThread* thread))
1194   zap_dead_java_or_native_locals(thread, is_java_frame);
1195 JRT_END
1196 
1197 // The following does not work because for one thing, the
1198 // thread state is wrong; it expects java, but it is native.
1199 // Also, the invariants in a native stub are different and
1200 // I'm not sure it is safe to have a MachCalRuntimeDirectNode
1201 // in there.
1202 // So for now, we do not zap in native stubs.
1203 
1204 JRT_LEAF(void, OptoRuntime::zap_dead_native_locals_C(JavaThread* thread))
1205   zap_dead_java_or_native_locals(thread, is_native_frame);
1206 JRT_END
1207 
1208 # endif