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