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