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