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