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