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