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