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